noko_builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+ gem "nokogiri", "~> 1.5.0"
3
+ gem "activesupport", "~> 3.2.1"
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.8.0"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.1)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ diff-lcs (1.1.3)
8
+ git (1.2.5)
9
+ i18n (0.6.0)
10
+ jeweler (1.8.3)
11
+ bundler (~> 1.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ rdoc
15
+ json (1.6.5)
16
+ multi_json (1.0.4)
17
+ nokogiri (1.5.0)
18
+ rake (0.9.2.2)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ rspec (2.8.0)
22
+ rspec-core (~> 2.8.0)
23
+ rspec-expectations (~> 2.8.0)
24
+ rspec-mocks (~> 2.8.0)
25
+ rspec-core (2.8.0)
26
+ rspec-expectations (2.8.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.8.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ activesupport (~> 3.2.1)
35
+ bundler (~> 1.0.0)
36
+ jeweler (~> 1.8.3)
37
+ nokogiri (~> 1.5.0)
38
+ rdoc (~> 3.12)
39
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 frausto
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = NokoBuilder
2
+
3
+ NokoBuilder is a gem that makes building out xml using nokogiri even easier! It gives you the ability to add overwritable default values and attributes to each xml tag, while still maintaining all the stuff you get from the normal nokogiri xml builder.
4
+
5
+ == Examples
6
+
7
+ ===VALUES
8
+
9
+ The values for xml tags can be overwritten by passing the value as an option when creating the xml document.
10
+
11
+ builder = NokoBuilder.new(options) do |xml|
12
+ xml.One "default value"
13
+ xml.Two
14
+ end
15
+
16
+ If <tt>options = {}</tt> then <tt>builder.to_xml</tt> would produce:
17
+
18
+ <?xml version="1.0"?>
19
+ <Doc>
20
+ <One>default value</One>
21
+ </Doc>
22
+
23
+ If <tt>options = {:One => "BOOM", :Two => "BAMO"}</tt> then <tt>builder.to_xml</tt> would produce:
24
+
25
+ <?xml version="1.0"?>
26
+ <Doc>
27
+ <One>BOOM</One>
28
+ <Two>BAMO</Two>
29
+ </Doc>
30
+
31
+ ===ATTRIBUTES
32
+
33
+ You can pass in attributes to the nokobuilder the same way you would with nokogiri builder.
34
+
35
+ builder = NokoBuilder.new(options) do |xml|
36
+ xml.One('xmlns' => 'http://stuff') do
37
+ xml.Inside "namey", :attribute => "me"
38
+ xml.Another(:one => "one", :two => "two")
39
+ end
40
+ end
41
+
42
+ If <tt>options = {}</tt> then <tt>builder.to_xml</tt> Would produce:
43
+
44
+ <?xml version="1.0"?>
45
+ <One xmlns="http://stuff">
46
+ <Inside attribute="me">namey</Inside>
47
+ <Another one="one" two="two"/>
48
+ </One>
49
+
50
+ If <tt>options = {:Inside_nattr => {:blamo => "one"}, :Another_nattr => {:one => 'new', :three => "thr"}}</tt> then <tt>builder.to_xml</tt> Would produce:
51
+
52
+ <?xml version="1.0"?>
53
+ <One xmlns="http://stuff">
54
+ <Inside attribute="me" blamo="one">namey</Inside>
55
+ <Another one="new" two="two" three="thr"/>
56
+ </One>
57
+
58
+ ===NOKOGIRI OPTIONS
59
+
60
+ You can pass in options to the nokogiri builder as the second argument if you want.
61
+
62
+ builder = NokoBuilder.new({},{:encoding => 'UTF-8'}) do |xml|
63
+ xml.One do
64
+ xml.Inside "namey"
65
+ end
66
+ end
67
+
68
+ Doing <tt>builder.to_xml</tt> Would produce:
69
+
70
+ <?xml version="1.0" encoding="UTF-8"?>
71
+ <One>
72
+ <Inside>namey</Inside>
73
+ </One>
74
+
75
+ == Contributing to noko_builder
76
+
77
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
78
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
79
+ * Fork the project.
80
+ * Start a feature/bugfix branch.
81
+ * Commit and push until you are happy with your contribution.
82
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
83
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
84
+
85
+ == Copyright
86
+
87
+ Copyright (c) 2012 frausto. See LICENSE.txt for
88
+ further details.
89
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "noko_builder"
18
+ gem.homepage = "http://github.com/frausto/noko_builder"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{a gem to make building out xmls with nokogiri easier}
21
+ gem.description = %Q{a gem that lets you add overwriteable values and attributes to the xmls you build}
22
+ gem.email = "nrfrausto@gmail.com"
23
+ gem.authors = ["frausto"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "noko_builder #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,45 @@
1
+ require 'nokogiri'
2
+ require 'active_support/all'
3
+
4
+ class NokoBuilder
5
+ attr_reader :noko
6
+ delegate *(Nokogiri::XML::Builder.new.methods - Object.new.methods), :to => :noko
7
+
8
+ def initialize(overwrite={}, noko_options={})
9
+ @overwrite = overwrite
10
+ @noko = Nokogiri::XML::Builder.new(noko_options) do |xml|
11
+ @xml = xml
12
+ yield self
13
+ end
14
+ end
15
+
16
+ def method_missing(id, *args, &block)
17
+ args = update_value(id, args)
18
+ args = update_attributes(id, args)
19
+ args = args.compact.reject(&:blank?)
20
+
21
+ @xml.send(id, *args, &block) if args.present? || block.present?
22
+ end
23
+
24
+ private
25
+
26
+ def update_value(id, args)
27
+ first_arg = args.shift
28
+ value = @overwrite[id.to_sym]
29
+
30
+ if value.present?
31
+ first_arg = first_arg.kind_of?(Hash) ? [value, first_arg] : [value]
32
+ end
33
+
34
+ [first_arg].flatten + args
35
+ end
36
+
37
+ def update_attributes(id, args)
38
+ attributes = @overwrite["#{id}_nattr".to_sym]
39
+ return args unless attributes.present?
40
+
41
+ args.map do |arg|
42
+ arg.kind_of?(Hash) ? arg.merge(attributes) : arg
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,161 @@
1
+ require 'spec_helper'
2
+
3
+ describe NokoBuilder do
4
+ describe "element value" do
5
+ it "should builder xml using the default values" do
6
+ builder = NokoBuilder.new do |xml|
7
+ xml.Sup "yo dawg"
8
+ end
9
+
10
+ builder.doc.xpath("//Sup").text.should == "yo dawg"
11
+ end
12
+
13
+ it "should overwrite defaults if specified" do
14
+ builder = NokoBuilder.new(:Sup => "cool story") do |xml|
15
+ xml.Sup "yo dawg"
16
+ end
17
+
18
+ builder.doc.xpath("//Sup").text.should == "cool story"
19
+ end
20
+
21
+ it "should use tag value even if there is no default specified" do
22
+ builder = NokoBuilder.new(:Sup => "what up") do |xml|
23
+ xml.Sup
24
+ end
25
+
26
+ builder.doc.xpath("//Sup").text.should == "what up"
27
+ end
28
+
29
+ it "should work with nested xml" do
30
+ builder = NokoBuilder.new(:Two => "what up") do |xml|
31
+ xml.Outside do
32
+ xml.One "first"
33
+ xml.Two "second"
34
+ end
35
+ end
36
+
37
+ builder.doc.xpath("//One").text.should == "first"
38
+ builder.doc.xpath("//Two").text.should == "what up"
39
+ end
40
+
41
+ it "should not include tag if not set and no defaults" do
42
+ builder = NokoBuilder.new do |xml|
43
+ xml.One
44
+ end
45
+
46
+ builder.to_xml.should_not =~ /One/
47
+ end
48
+
49
+ it "should work with namespacing" do
50
+ builder = NokoBuilder.new do |xml|
51
+ xml.One('xmlns' => 'http://stuff') do
52
+ xml.Inside "namey"
53
+ end
54
+ end
55
+
56
+ builder.doc.xpath("//ns:Inside", "ns" => "http://stuff").text.should == "namey"
57
+ end
58
+
59
+ it "should be able to pass options to nokogiri builder" do
60
+ builder = NokoBuilder.new({},{:encoding => 'UTF-8'}) do |xml|
61
+ xml.One('xmlns' => 'http://stuff') do
62
+ xml.Inside "namey"
63
+ end
64
+ end
65
+
66
+ builder.to_xml.should =~ /\<\?xml version="1.0" encoding="UTF-8"\?\>/
67
+ end
68
+ end
69
+
70
+ describe "attributes" do
71
+ it "should work normally like nokogiri builder with attributes" do
72
+ builder = NokoBuilder.new do |xml|
73
+ xml.Outside do
74
+ xml.One "no def", :attr => "cool"
75
+ end
76
+ end
77
+
78
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
79
+ end
80
+
81
+ it "should keep attributes even if text value is being overwritten" do
82
+ builder = NokoBuilder.new(:One => "over") do |xml|
83
+ xml.Outside do
84
+ xml.One "no def", :attr => "cool"
85
+ end
86
+ end
87
+
88
+ builder.doc.xpath("//One").text.should == "over"
89
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
90
+ end
91
+
92
+ it "should keep attributes even if text value is being overwritten from blank" do
93
+ builder = NokoBuilder.new(:One => "over") do |xml|
94
+ xml.Outside do
95
+ xml.One :attr => "cool"
96
+ end
97
+ end
98
+
99
+ builder.doc.xpath("//One").text.should == "over"
100
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
101
+ end
102
+
103
+ it "should overwrite attributes" do
104
+ builder = NokoBuilder.new(:One_nattr => {:another => 'awesome'}) do |xml|
105
+ xml.Outside do
106
+ xml.One :attr => "cool", :another => "beans"
107
+ end
108
+ end
109
+
110
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
111
+ builder.doc.xpath("//One").attr('another').text.should == "awesome"
112
+ end
113
+
114
+ it "should add attributes" do
115
+ builder = NokoBuilder.new(:One_nattr => {:another => 'awesome', :newone => 'new'}) do |xml|
116
+ xml.Outside do
117
+ xml.One :another => "beans"
118
+ end
119
+ end
120
+
121
+ builder.doc.xpath("//One").attr('newone').text.should == "new"
122
+ builder.doc.xpath("//One").attr('another').text.should == "awesome"
123
+ end
124
+
125
+ it "should overwrite attributes and value" do
126
+ builder = NokoBuilder.new(:One => "test", :One_nattr => {:another => 'awesome'}) do |xml|
127
+ xml.Outside do
128
+ xml.One :attr => "cool", :another => "beans"
129
+ end
130
+ end
131
+
132
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
133
+ builder.doc.xpath("//One").attr('another').text.should == "awesome"
134
+ builder.doc.xpath("//One").text.should == "test"
135
+ end
136
+
137
+ it "should overwrite attributes and value when having a default value" do
138
+ builder = NokoBuilder.new(:One => "test", :One_nattr => {:another => 'awesome'}) do |xml|
139
+ xml.Outside do
140
+ xml.One "testinit", :attr => "cool", :another => "beans"
141
+ end
142
+ end
143
+
144
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
145
+ builder.doc.xpath("//One").attr('another').text.should == "awesome"
146
+ builder.doc.xpath("//One").text.should == "test"
147
+ end
148
+
149
+ it "should overwrite attributes when having a default value" do
150
+ builder = NokoBuilder.new(:One_nattr => {:another => 'awesome'}) do |xml|
151
+ xml.Outside do
152
+ xml.One "def", :attr => "cool", :another => "beans"
153
+ end
154
+ end
155
+
156
+ builder.doc.xpath("//One").attr('attr').text.should == "cool"
157
+ builder.doc.xpath("//One").attr('another').text.should == "awesome"
158
+ builder.doc.xpath("//One").text.should == "def"
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'noko_builder'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noko_builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - frausto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-12 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.8.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rdoc
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: "3.12"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.0.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: jeweler
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ description: a gem that lets you add overwriteable values and attributes to the xmls you build
82
+ email: nrfrausto@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - .rspec
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - lib/noko_builder.rb
100
+ - spec/noko_builder_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/frausto/noko_builder
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 175878911612147478
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.15
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: a gem to make building out xmls with nokogiri easier
132
+ test_files: []
133
+