ox_builder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +11 -0
- data/lib/ox/builder.rb +18 -0
- data/lib/ox/builder/extensions.rb +28 -0
- data/lib/ox/builder/handler.rb +20 -0
- data/lib/ox/builder/proxy.rb +74 -0
- data/lib/ox/builder/version.rb +5 -0
- data/lib/ox_builder.rb +5 -0
- data/ox_builder.gemspec +24 -0
- data/spec/builder_spec.rb +43 -0
- data/spec/spec_helper.rb +6 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
M2IzYjc5OGYxMDE5ZDI4ZTU1YWU3Y2UwNDg2Njk5NGRmYmVmYTc1YQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjBmMDI0NTFkY2Q5ODFiMWMzMGQwNzFkMjMzN2M4ZmFkNTI0MWI5ZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YmU5MmI2ZDI0OTY3YWIzOWIzNzRmYjc0NTdmODFiMWQ4M2YzYjczM2RlNjNh
|
10
|
+
NzQ3ZDk5NWU0MTMzNTExODViODJhN2VlMWMwZjkwMjI3MGI3MGNkZGNlZGEy
|
11
|
+
M2I4ZjAwMmEwNjVlZGQwNTBjMDQ4MmQzZjU5MzlkM2YzZTdmZmQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YWJlOTU3OGQxOTUxOWZmZTQwYWI1NzIyZmRlMGQzOGYyMzhmMWY1NmEzMmFh
|
14
|
+
NzlkMjEwODM2NmI4ZjA4MTU4NzhlMWNmZjhhZWZhODg3YmRjNjkwZWJhY2Vk
|
15
|
+
YmFmMTM0MDNiZmI5MzI2YjlhMTY3OWE4OGNiMTM0Y2Q4YjIzY2Q=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@ox_builder --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 undr
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# OxBuilder
|
2
|
+
|
3
|
+
XML builder with using ox. Fast and convenient DSL.
|
4
|
+
|
5
|
+
[](http://travis-ci.org/undr/ox_builder)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ox_builder'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ox_builder
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Ox::Builder.document do |document|
|
25
|
+
document.element(
|
26
|
+
'rss', 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
|
27
|
+
'xmlns:media' => 'http://search.yahoo.com/mrss/', 'xmlns:content' => 'http://purl.org/rss/1.0/modules/content/',
|
28
|
+
'xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd', 'version' => '2.0'
|
29
|
+
) do |rss|
|
30
|
+
rss.element('channel') do |channel|
|
31
|
+
description(channel)
|
32
|
+
|
33
|
+
Post.all do |post|
|
34
|
+
channel_item(post)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def description node
|
41
|
+
node.element('title', 'Title of channel')
|
42
|
+
node.element('link', 'http://www.example.com')
|
43
|
+
node.element('description', 'Description of channel')
|
44
|
+
node.element('language', 'ru')
|
45
|
+
node.element('copyright', 'copyright')
|
46
|
+
end
|
47
|
+
|
48
|
+
def channel_post node, post
|
49
|
+
node.element('item') do |item|
|
50
|
+
node.element('title', post.title)
|
51
|
+
node.element('link', post.permalink)
|
52
|
+
node.element('guid', post.permalink)
|
53
|
+
node.element('pubDate', post.published_at)
|
54
|
+
node.element('description', post.description)
|
55
|
+
node.element('category', post.category)
|
56
|
+
mode.element('enclosure', url: post.image.url, type: 'image/jpeg', length: post.image.size)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake'
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../..", __FILE__)
|
4
|
+
Dir[File.join('lib', 'tasks', '**', '*.rake')].each{|file| load file}
|
5
|
+
#Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
require 'rspec/core'
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:spec)
|
11
|
+
task :default => [:spec]
|
data/lib/ox/builder.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'ox/builder/proxy'
|
2
|
+
|
3
|
+
module Ox::Builder
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def document attributes = {}, options = {}
|
7
|
+
attributes = default_attributes.merge(attributes.stringify_keys)
|
8
|
+
Proxy.new(Ox::Document.new(attributes)).tap do |doc|
|
9
|
+
doc.instruct('xml', attributes)
|
10
|
+
yield doc if block_given?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def default_attributes
|
16
|
+
{'version' => '1.0', 'encoding' => 'UTF-8'}
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
unless defined?(ActiveSupport)
|
2
|
+
class Hash
|
3
|
+
def extractable_options?
|
4
|
+
instance_of?(Hash)
|
5
|
+
end
|
6
|
+
|
7
|
+
def stringify_keys
|
8
|
+
dup.stringify_keys!
|
9
|
+
end
|
10
|
+
|
11
|
+
def stringify_keys!
|
12
|
+
keys.each do |key|
|
13
|
+
self[key.to_s] = delete(key)
|
14
|
+
end
|
15
|
+
self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Array
|
20
|
+
def extract_options!
|
21
|
+
if last.is_a?(Hash) && last.extractable_options?
|
22
|
+
pop
|
23
|
+
else
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
if defined?(ActionView)
|
2
|
+
module Ox::Builder
|
3
|
+
class Handler
|
4
|
+
class_attribute :default_format
|
5
|
+
self.default_format = Mime::XML
|
6
|
+
|
7
|
+
def call template
|
8
|
+
<<-SOURCE
|
9
|
+
(
|
10
|
+
Ox::Builder.document do |xml|
|
11
|
+
#{template.source}
|
12
|
+
end
|
13
|
+
).to_s;
|
14
|
+
SOURCE
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ActionView::Template.register_template_handler :ox, Ox::Builder::Handler.new
|
20
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Ox::Builder
|
2
|
+
class Proxy
|
3
|
+
OX_METHODS = %W{
|
4
|
+
value eql? attributes [] []= << text nodes alocate locate content root
|
5
|
+
}.map(&:to_sym).freeze
|
6
|
+
|
7
|
+
OX_METHODS.each do |method|
|
8
|
+
module_eval(<<-EOS)
|
9
|
+
def #{method}(*args, &block)
|
10
|
+
if node || node.respond_to?(:#{method})
|
11
|
+
node.__send__(:#{method}, *args, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
EOS
|
15
|
+
end
|
16
|
+
|
17
|
+
# delegate *OX_METHODS, to: :node
|
18
|
+
|
19
|
+
attr_reader :node
|
20
|
+
|
21
|
+
def initialize node
|
22
|
+
@node = node
|
23
|
+
end
|
24
|
+
|
25
|
+
def element *args
|
26
|
+
attributes = args.extract_options!
|
27
|
+
name = args.shift
|
28
|
+
|
29
|
+
proxify_node(Ox::Element, name) do |proxy|
|
30
|
+
proxy.add_attributes(attributes)
|
31
|
+
args.each do |text|
|
32
|
+
proxy.node << text.to_s if text.respond_to?(:to_s)
|
33
|
+
end
|
34
|
+
yield proxy if block_given?
|
35
|
+
node << proxy.node
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def instruct name, attributes = {}
|
40
|
+
proxify_node(Ox::Instruct, name) do |proxy|
|
41
|
+
proxy.add_attributes(attributes)
|
42
|
+
node << proxy.node
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def cdata content
|
47
|
+
proxify_node(Ox::CData, content) do |proxy|
|
48
|
+
node << proxy.node
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def comment content
|
53
|
+
proxify_node(Ox::Comment, content) do |proxy|
|
54
|
+
node << proxy.node
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_attributes attributes
|
59
|
+
attributes.keys.each do |attribute|
|
60
|
+
node[attribute] = attributes[attribute]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_s
|
65
|
+
Ox.dump(node)
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def proxify_node klass, *args, &block
|
71
|
+
Proxy.new(klass.new(*args)).tap(&block)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/ox_builder.rb
ADDED
data/ox_builder.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ox/builder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ox_builder"
|
8
|
+
gem.version = Ox::Builder::VERSION
|
9
|
+
gem.authors = ["undr"]
|
10
|
+
gem.email = ["undr@yandex.ru"]
|
11
|
+
gem.description = %q{XML builder with using ox}
|
12
|
+
gem.summary = %q{XML builder with using ox. Fast and convenient DSL.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency 'rake'
|
21
|
+
gem.add_runtime_dependency 'ox'
|
22
|
+
gem.add_development_dependency 'pry'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ox::Builder do
|
4
|
+
context do
|
5
|
+
subject{Ox::Builder.document}
|
6
|
+
|
7
|
+
it 'should create Ox:Document with Ox::Instruct' do
|
8
|
+
subject.should be_instance_of(Ox::Builder::Proxy)
|
9
|
+
subject.node.should be_instance_of(Ox::Document)
|
10
|
+
subject.to_s.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context do
|
15
|
+
subject{Ox::Builder.document(version: '1.1', 'encoding' => 'cp1251')}
|
16
|
+
|
17
|
+
it 'should create Ox:Document with Ox::Instruct with attributes' do
|
18
|
+
subject.should be_instance_of(Ox::Builder::Proxy)
|
19
|
+
subject.node.should be_instance_of(Ox::Document)
|
20
|
+
subject.to_s.should == "<?xml version=\"1.1\" encoding=\"cp1251\"?>\n"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context do
|
25
|
+
subject do
|
26
|
+
Ox::Builder.document do |doc|
|
27
|
+
doc.element('RootNode', key1: 'value #1', key2: 'Value #2') do |root|
|
28
|
+
root.comment('Commented text')
|
29
|
+
root.element('Node') do |node|
|
30
|
+
node.cdata('Safety text')
|
31
|
+
end
|
32
|
+
root.element('AnotherNode') do |n|
|
33
|
+
n.element('x:Child', 'Node Text', attr1: 'Attr #1', attr2: 'Attr #2')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should create XML' do
|
40
|
+
subject.to_s.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RootNode key1=\"value #1\" key2=\"Value #2\">\n <!-- Commented text -->\n <Node>\n <![CDATA[Safety text]]>\n </Node>\n <AnotherNode>\n <x:Child attr1=\"Attr #1\" attr2=\"Attr #2\">Node Text</x:Child>\n </AnotherNode>\n</RootNode>\n"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ox_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- undr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ox
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: XML builder with using ox
|
70
|
+
email:
|
71
|
+
- undr@yandex.ru
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .rvmrc
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/ox/builder.rb
|
85
|
+
- lib/ox/builder/extensions.rb
|
86
|
+
- lib/ox/builder/handler.rb
|
87
|
+
- lib/ox/builder/proxy.rb
|
88
|
+
- lib/ox/builder/version.rb
|
89
|
+
- lib/ox_builder.rb
|
90
|
+
- ox_builder.gemspec
|
91
|
+
- spec/builder_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: ''
|
94
|
+
licenses: []
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.0.7
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: XML builder with using ox. Fast and convenient DSL.
|
116
|
+
test_files:
|
117
|
+
- spec/builder_spec.rb
|
118
|
+
- spec/spec_helper.rb
|