gyoku 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ script: "rake"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - ree
6
+ - rbx
7
+ - jruby
@@ -0,0 +1,55 @@
1
+ ## 0.4.4
2
+
3
+ * Fix: [issue 6](https://github.com/rubiii/gyoku/issues/6) -
4
+ `Gyoku.xml` does not modify the original Hash.
5
+
6
+ ## 0.4.3
7
+
8
+ * Fix: Make sure `require "date"` when necessary.
9
+
10
+ ## 0.4.2
11
+
12
+ * Fix: `Array.to_xml` so that the given :namespace is applied to every element
13
+ in an Array.
14
+
15
+ ## 0.4.1
16
+
17
+ * Fix: Alternative formulas and namespaces.
18
+
19
+ ## 0.4.0
20
+
21
+ * Feature: Added alternative Symbol conversion formulas. You can choose between
22
+ :lower_camelcase (the default), :camelcase and :none.
23
+
24
+ Gyoku.convert_symbols_to :camelcase
25
+
26
+ You can even define your own formula:
27
+
28
+ Gyoku.convert_symbols_to { |key| key.upcase }
29
+
30
+ ## 0.3.1
31
+
32
+ * Feature: Gyoku now calls Proc objects and converts their return value.
33
+
34
+ ## 0.3.0
35
+
36
+ * Feature: Now when all Hash keys need to be namespaced (like with
37
+ elementFormDefault), you can use options to to trigger this behavior.
38
+
39
+ Gyoku.xml hash,
40
+ :element_form_default => :qualified,
41
+ :namespace => :v2
42
+
43
+ ## 0.2.0
44
+
45
+ * Feature: Added support for self-closing tags. Hash keys ending with a forward
46
+ slash (regardless of their value) are now converted to self-closing tags.
47
+
48
+ ## 0.1.1
49
+
50
+ * Fix: Allow people to use new versions of builder.
51
+
52
+ ## 0.1.0
53
+
54
+ * Initial version. Gyoku was born as a core extension inside the
55
+ [Savon](http://rubygems.org/gems/savon) library.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Gyoku
1
+ Gyoku ![http://travis-ci.org/rubiii/gyoku](http://travis-ci.org/rubiii/gyoku.png)
2
2
  =====
3
3
 
4
4
  Gyoku translates Ruby Hashes to XML.
data/Rakefile CHANGED
@@ -1,42 +1,10 @@
1
1
  require "bundler"
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
- begin
5
- require "yard"
4
+ require "rspec/core/rake_task"
6
5
 
7
- YARD::Rake::YardocTask.new do |t|
8
- t.files = ["README.md", "lib/**/*.rb"]
9
- end
10
- rescue LoadError
11
- desc message = %{"gem install yard" to generate documentation}
12
- task("yard") { abort message }
13
- end
14
-
15
- begin
16
- require "metric_fu"
17
-
18
- MetricFu::Configuration.run do |c|
19
- c.metrics = [:churn, :flog, :flay, :reek, :roodi, :saikuro] # :rcov seems to be broken
20
- c.graphs = [:flog, :flay, :reek, :roodi]
21
- c.flay = { :dirs_to_flay => ["lib"], :minimum_score => 20 }
22
- c.rcov[:rcov_opts] << "-Ilib -Ispec"
23
- end
24
- rescue LoadError
25
- desc message = %{"gem install metric_fu" to generate metrics}
26
- task("metrics:all") { abort message }
27
- end
28
-
29
- begin
30
- require "rspec/core/rake_task"
31
-
32
- RSpec::Core::RakeTask.new do |t|
33
- t.pattern = "spec/**/*_spec.rb"
34
- t.rspec_opts = %w(-fd -c)
35
- end
36
- rescue LoadError
37
- task :spec do
38
- abort "Run 'gem install rspec' to be able to run specs"
39
- end
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = %w(-c)
40
8
  end
41
9
 
42
10
  task :default => :spec
@@ -6,7 +6,7 @@ module Gyoku
6
6
 
7
7
  # Translates a given +hash+ with +options+ to XML.
8
8
  def xml(hash, options = {})
9
- Hash.to_xml hash, options
9
+ Hash.to_xml hash.dup, options
10
10
  end
11
11
 
12
12
  # Yields this object for configuration.
@@ -1,5 +1,5 @@
1
1
  module Gyoku
2
2
 
3
- VERSION = "0.4.3"
3
+ VERSION = "0.4.4"
4
4
 
5
5
  end
@@ -3,45 +3,45 @@ require "spec_helper"
3
3
  describe Gyoku::Array do
4
4
 
5
5
  describe ".to_xml" do
6
- it "should return the XML for an Array of Hashes" do
6
+ it "returns the XML for an Array of Hashes" do
7
7
  array = [{ :name => "adam" }, { :name => "eve" }]
8
8
  result = "<user><name>adam</name></user><user><name>eve</name></user>"
9
-
9
+
10
10
  to_xml(array, "user").should == result
11
11
  end
12
12
 
13
- it "should return the XML for an Array of different Objects" do
13
+ it "returns the XML for an Array of different Objects" do
14
14
  array = [:symbol, "string", 123]
15
15
  result = "<value>symbol</value><value>string</value><value>123</value>"
16
-
16
+
17
17
  to_xml(array, "value").should == result
18
18
  end
19
19
 
20
- it "should default to escape special characters" do
20
+ it "defaults to escape special characters" do
21
21
  array = ["<tag />", "adam & eve"]
22
22
  result = "<value>&lt;tag /&gt;</value><value>adam &amp; eve</value>"
23
-
23
+
24
24
  to_xml(array, "value").should == result
25
25
  end
26
26
 
27
- it "should not escape special characters when told to" do
27
+ it "does not escape special characters when told to" do
28
28
  array = ["<tag />", "adam & eve"]
29
29
  result = "<value><tag /></value><value>adam & eve</value>"
30
-
30
+
31
31
  to_xml(array, "value", false).should == result
32
32
  end
33
33
 
34
- it "should add attributes to a given tag" do
34
+ it "adds attributes to a given tag" do
35
35
  array = ["adam", "eve"]
36
36
  result = '<value active="true">adam</value><value active="true">eve</value>'
37
-
37
+
38
38
  to_xml(array, "value", :escape_xml, :active => true).should == result
39
39
  end
40
40
 
41
- it "should add attributes to duplicate tags" do
41
+ it "adds attributes to duplicate tags" do
42
42
  array = ["adam", "eve"]
43
43
  result = '<value id="1">adam</value><value id="2">eve</value>'
44
-
44
+
45
45
  to_xml(array, "value", :escape_xml, :id => [1, 2]).should == result
46
46
  end
47
47
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Gyoku::Hash do
4
4
 
5
5
  describe ".to_xml" do
6
- describe "should return SOAP request compatible XML" do
6
+ describe "returns SOAP request compatible XML" do
7
7
  it "for a simple Hash" do
8
8
  to_xml(:some => "user").should == "<some>user</some>"
9
9
  end
@@ -29,20 +29,20 @@ describe Gyoku::Hash do
29
29
  end
30
30
  end
31
31
 
32
- it "should convert Hash key Symbols to lowerCamelCase" do
32
+ it "converts Hash key Symbols to lowerCamelCase" do
33
33
  to_xml(:find_or_create => "user").should == "<findOrCreate>user</findOrCreate>"
34
34
  end
35
35
 
36
- it "should not convert Hash key Strings" do
36
+ it "does not convert Hash key Strings" do
37
37
  to_xml("find_or_create" => "user").should == "<find_or_create>user</find_or_create>"
38
38
  end
39
39
 
40
- it "should convert DateTime objects to xs:dateTime compliant Strings" do
40
+ it "converts DateTime objects to xs:dateTime compliant Strings" do
41
41
  to_xml(:before => DateTime.new(2012, 03, 22, 16, 22, 33)).
42
42
  should == "<before>2012-03-22T16:22:33+00:00</before>"
43
43
  end
44
44
 
45
- it "should convert Objects responding to to_datetime to xs:dateTime compliant Strings" do
45
+ it "converts Objects responding to to_datetime to xs:dateTime compliant Strings" do
46
46
  singleton = Object.new
47
47
  def singleton.to_datetime
48
48
  DateTime.new(2012, 03, 22, 16, 22, 33)
@@ -51,40 +51,40 @@ describe Gyoku::Hash do
51
51
  to_xml(:before => singleton).should == "<before>2012-03-22T16:22:33+00:00</before>"
52
52
  end
53
53
 
54
- it "should call to_s on Strings even if they respond to to_datetime" do
54
+ it "calls to_s on Strings even if they respond to to_datetime" do
55
55
  object = "gorilla"
56
56
  object.expects(:to_datetime).never
57
57
 
58
58
  to_xml(:name => object).should == "<name>gorilla</name>"
59
59
  end
60
60
 
61
- it "should properly serialize nil values" do
61
+ it "properly serializes nil values" do
62
62
  to_xml(:some => nil).should == '<some xsi:nil="true"/>'
63
63
  end
64
64
 
65
- it "should create self-closing tags for Hash keys ending with a forward slash" do
65
+ it "creates self-closing tags for Hash keys ending with a forward slash" do
66
66
  to_xml("self-closing/" => nil).should == '<self-closing/>'
67
67
  end
68
68
 
69
- it "should call to_s on any other Object" do
69
+ it "calls to_s on any other Object" do
70
70
  [666, true, false].each do |object|
71
71
  to_xml(:some => object).should == "<some>#{object}</some>"
72
72
  end
73
73
  end
74
74
 
75
- it "should default to escape special characters" do
75
+ it "defaults to escape special characters" do
76
76
  result = to_xml(:some => { :nested => "<tag />" }, :tag => "<tag />")
77
77
  result.should include("<tag>&lt;tag /&gt;</tag>")
78
78
  result.should include("<some><nested>&lt;tag /&gt;</nested></some>")
79
79
  end
80
80
 
81
- it "should not escape special characters for keys marked with an exclamation mark" do
81
+ it "does not escape special characters for keys marked with an exclamation mark" do
82
82
  result = to_xml(:some => { :nested! => "<tag />" }, :tag! => "<tag />")
83
83
  result.should include("<tag><tag /></tag>")
84
84
  result.should include("<some><nested><tag /></nested></some>")
85
85
  end
86
86
 
87
- it "should preserve the order of Hash keys and values specified through :order!" do
87
+ it "preserves the order of Hash keys and values specified through :order!" do
88
88
  hash = { :find_user => { :name => "Lucy", :id => 666, :order! => [:id, :name] } }
89
89
  result = "<findUser><id>666</id><name>Lucy</name></findUser>"
90
90
  to_xml(hash).should == result
@@ -94,15 +94,17 @@ describe Gyoku::Hash do
94
94
  to_xml(hash).should == result
95
95
  end
96
96
 
97
- it "should raise an error if the :order! Array does not match the Hash keys" do
97
+ it "raises if the :order! Array is missing Hash keys" do
98
98
  hash = { :name => "Lucy", :id => 666, :order! => [:name] }
99
- lambda { to_xml(hash) }.should raise_error(ArgumentError)
99
+ lambda { to_xml(hash) }.should raise_error(ArgumentError, "Missing elements in :order! [:id]")
100
+ end
100
101
 
101
- hash = { :by_name => { :name => "Lucy", :lname => "Sky", :order! => [:mname, :name] } }
102
- lambda { to_xml(hash) }.should raise_error(ArgumentError)
102
+ it "raises if the :order! Array contains missing Hash keys" do
103
+ hash = { :by_name => { :first_name => "Lucy", :last_name => "Sky", :order! => [:first_name, :middle_name, :last_name] } }
104
+ lambda { to_xml(hash) }.should raise_error(ArgumentError, "Spurious elements in :order! [:middle_name]")
103
105
  end
104
106
 
105
- it "should add attributes to Hash keys specified through :attributes!" do
107
+ it "adds attributes to Hash keys specified through :attributes!" do
106
108
  hash = { :find_user => { :person => "Lucy", :attributes! => { :person => { :id => 666 } } } }
107
109
  result = '<findUser><person id="666">Lucy</person></findUser>'
108
110
  to_xml(hash).should == result
@@ -111,7 +113,7 @@ describe Gyoku::Hash do
111
113
  to_xml(hash).should include('id="666"', 'city="Hamburg"')
112
114
  end
113
115
 
114
- it "should add attributes to duplicate Hash keys specified through :attributes!" do
116
+ it "adds attributes to duplicate Hash keys specified through :attributes!" do
115
117
  hash = { :find_user => { :person => ["Lucy", "Anna"], :attributes! => { :person => { :id => [1, 3] } } } }
116
118
  result = '<findUser><person id="1">Lucy</person><person id="3">Anna</person></findUser>'
117
119
  to_xml(hash).should == result
@@ -121,25 +123,33 @@ describe Gyoku::Hash do
121
123
  to_xml(hash).should == result
122
124
  end
123
125
 
126
+ it "adds attributes to self-closing tags" do
127
+ hash = {
128
+ "category/" => "",
129
+ :attributes! => { "category/" => { :id => 1 } }
130
+ }
131
+
132
+ to_xml(hash).should == '<category id="1"/>'
133
+ end
134
+
124
135
  context "with :element_form_default set to :qualified and a :namespace" do
125
- it "should add the given :namespace to every element" do
126
- hash = { :first => { "first_name" => "Luvy" }, ":second" => { :":first_name" => "Anna" }, "v2:third" => { "v2:firstName" => "Danie" } }
136
+ it "adds the given :namespace to every element" do
137
+ hash = { :first => { "first_name" => "Lucy" }, ":second" => { :":first_name" => "Anna" }, "v2:third" => { "v2:firstName" => "Danie" } }
127
138
  result = to_xml hash, :element_form_default => :qualified, :namespace => :v1
128
139
 
129
140
  result.should include(
130
- "<v1:first><v1:first_name>Luvy</v1:first_name></v1:first>",
141
+ "<v1:first><v1:first_name>Lucy</v1:first_name></v1:first>",
131
142
  "<second><firstName>Anna</firstName></second>",
132
143
  "<v2:third><v2:firstName>Danie</v2:firstName></v2:third>"
133
144
  )
134
145
  end
135
146
 
136
- it "should add given :namespace to every element in an array" do
137
- hash = { :array => [ :first => "Luvy", :second => "Anna" ]}
147
+ it "adds given :namespace to every element in an array" do
148
+ hash = { :array => [ :first => "Lucy", :second => "Anna" ]}
138
149
  result = to_xml hash, :element_form_default => :qualified, :namespace => :v1
139
150
 
140
- result.should include("<v1:array>", "<v1:first>Luvy</v1:first>", "<v1:second>Anna</v1:second>")
151
+ result.should include("<v1:array>", "<v1:first>Lucy</v1:first>", "<v1:second>Anna</v1:second>")
141
152
  end
142
-
143
153
  end
144
154
  end
145
155
 
@@ -3,35 +3,35 @@ require "spec_helper"
3
3
  describe Gyoku::XMLKey do
4
4
 
5
5
  describe ".create" do
6
- it "should remove exclamation marks from the end of a String" do
6
+ it "removes exclamation marks from the end of a String" do
7
7
  create("value!").should == "value"
8
8
  end
9
9
 
10
- it "should remove forward slashes from the end of a String" do
10
+ it "removes forward slashes from the end of a String" do
11
11
  create("self-closing/").should == "self-closing"
12
12
  end
13
13
 
14
- it "should not convert snake_case Strings" do
14
+ it "does not convert snake_case Strings" do
15
15
  create("lower_camel_case").should == "lower_camel_case"
16
16
  end
17
17
 
18
- it "should convert snake_case Symbols to lowerCamelCase Strings" do
18
+ it "converts snake_case Symbols to lowerCamelCase Strings" do
19
19
  create(:lower_camel_case).should == "lowerCamelCase"
20
20
  create(:lower_camel_case!).should == "lowerCamelCase"
21
21
  end
22
22
 
23
23
  context "with :element_form_default set to :qualified and a :namespace" do
24
- it "should add the given namespace" do
24
+ it "adds the given namespace" do
25
25
  key = create :qualify, :element_form_default => :qualified, :namespace => :v1
26
26
  key.should == "v1:qualify"
27
27
  end
28
28
 
29
- it "should not add the given namespace if the key starts with a colon" do
29
+ it "does not add the given namespace if the key starts with a colon" do
30
30
  key = create ":qualify", :element_form_default => :qualified, :namespace => :v1
31
31
  key.should == "qualify"
32
32
  end
33
33
 
34
- it "should add a given :namespace after converting the key" do
34
+ it "adds a given :namespace after converting the key" do
35
35
  Gyoku::XMLKey.symbol_converter = :camelcase
36
36
 
37
37
  key = create :username, :element_form_default => :qualified, :namespace => :v1
@@ -45,26 +45,26 @@ describe Gyoku::XMLKey do
45
45
  describe ".symbol_converter" do
46
46
  after { Gyoku::XMLKey.symbol_converter = :lower_camelcase } #reset
47
47
 
48
- it "should return the default lower_camelcase converter" do
48
+ it "returns the default lower_camelcase converter" do
49
49
  Gyoku::XMLKey.symbol_converter.call("snake_case").should == "snakeCase"
50
50
  end
51
51
 
52
- it "should accept :lower_camelcase" do
52
+ it "accepts :lower_camelcase" do
53
53
  Gyoku::XMLKey.symbol_converter = :lower_camelcase
54
54
  Gyoku::XMLKey.create(:snake_case).should == "snakeCase"
55
55
  end
56
56
 
57
- it "should accept :camelcase" do
57
+ it "accepts :camelcase" do
58
58
  Gyoku::XMLKey.symbol_converter = :camelcase
59
59
  Gyoku::XMLKey.create(:snake_case).should == "SnakeCase"
60
60
  end
61
61
 
62
- it "should accept :none" do
62
+ it "accepts :none" do
63
63
  Gyoku::XMLKey.symbol_converter = :none
64
64
  Gyoku::XMLKey.create(:snake_Case).should == "snake_Case"
65
65
  end
66
66
 
67
- it "should allow to set a custom converter" do
67
+ it "allows to set a custom converter" do
68
68
  Gyoku::XMLKey.symbol_converter = Proc.new { |key| key.upcase }
69
69
  Gyoku::XMLKey.create(:snake_case).should == "SNAKE_CASE"
70
70
  end
@@ -7,23 +7,23 @@ describe Gyoku::XMLValue do
7
7
 
8
8
  describe ".create" do
9
9
  context "for DateTime objects" do
10
- it "should return an xs:dateTime compliant String" do
10
+ it "returns an xs:dateTime compliant String" do
11
11
  create(datetime).should == datetime_string
12
12
  end
13
13
  end
14
14
 
15
- it "should return the String value and escape special characters" do
15
+ it "returns the String value and escapes special characters" do
16
16
  create("string").should == "string"
17
17
  create("<tag>").should == "&lt;tag&gt;"
18
18
  create("at&t").should == "at&amp;t"
19
19
  create('"quotes"').should == "&quot;quotes&quot;"
20
20
  end
21
21
 
22
- it "should just return the String value without escaping special characters" do
22
+ it "returns the String value without escaping special characters" do
23
23
  create("<tag>", false).should == "<tag>"
24
24
  end
25
25
 
26
- it "should return an xs:dateTime compliant String for Objects responding to #to_datetime" do
26
+ it "returns an xs:dateTime compliant String for Objects responding to #to_datetime" do
27
27
  singleton = Object.new
28
28
  def singleton.to_datetime
29
29
  DateTime.new 2012, 03, 22, 16, 22, 33
@@ -32,12 +32,12 @@ describe Gyoku::XMLValue do
32
32
  create(singleton).should == "2012-03-22T16:22:33+00:00"
33
33
  end
34
34
 
35
- it "should #call Proc objects and convert their return value" do
35
+ it "calls Proc objects and converts their return value" do
36
36
  object = lambda { DateTime.new 2012, 03, 22, 16, 22, 33 }
37
37
  create(object).should == "2012-03-22T16:22:33+00:00"
38
38
  end
39
39
 
40
- it "should call #to_s unless the Object responds to #to_datetime" do
40
+ it "calls #to_s unless the Object responds to #to_datetime" do
41
41
  create("value").should == "value"
42
42
  end
43
43
  end
@@ -3,14 +3,28 @@ require "spec_helper"
3
3
  describe Gyoku do
4
4
 
5
5
  describe ".xml" do
6
- it "should translate a given Hash to XML" do
7
- Gyoku::Hash.expects(:to_xml).with({:id => 1}, :element_form_default => :qualified)
8
- Gyoku.xml({ :id => 1 }, :element_form_default => :qualified)
6
+ it "translates a given Hash to XML" do
7
+ Gyoku.xml({ :id => 1 }, :element_form_default => :qualified).should == "<id>1</id>"
8
+ end
9
+
10
+ it "does not modify the original Hash" do
11
+ hash = {
12
+ :person => {
13
+ :first_name => "Lucy",
14
+ :last_name => "Sky",
15
+ :order! => [:first_name, :last_name]
16
+ },
17
+ :attributes! => { :person => { :id => "666" } }
18
+ }
19
+ original_hash = hash.dup
20
+
21
+ Gyoku.xml(hash)
22
+ original_hash.should == hash
9
23
  end
10
24
  end
11
25
 
12
26
  describe ".configure" do
13
- it "should yield the Gyoku module" do
27
+ it "yields the Gyoku module" do
14
28
  Gyoku.configure { |config| config.should respond_to(:convert_symbols_to) }
15
29
  end
16
30
  end
@@ -18,12 +32,12 @@ describe Gyoku do
18
32
  describe ".convert_symbols_to" do
19
33
  after { Gyoku.convert_symbols_to(:lower_camelcase) } # reset
20
34
 
21
- it "should accept a predefined formula" do
35
+ it "accepts a predefined formula" do
22
36
  Gyoku.convert_symbols_to(:camelcase)
23
37
  Gyoku::XMLKey.create(:snake_case).should == "SnakeCase"
24
38
  end
25
39
 
26
- it "should accept a block" do
40
+ it "accepts a block" do
27
41
  Gyoku.convert_symbols_to { |key| key.upcase }
28
42
  Gyoku::XMLKey.create(:snake_case).should == "SNAKE_CASE"
29
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gyoku
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Harrington
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-04 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-05-15 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: builder
@@ -92,6 +91,8 @@ files:
92
91
  - .gemtest
93
92
  - .gitignore
94
93
  - .rspec
94
+ - .travis.yml
95
+ - CHANGELOG.md
95
96
  - Gemfile
96
97
  - LICENSE
97
98
  - README.md
@@ -111,7 +112,6 @@ files:
111
112
  - spec/gyoku/xml_value_spec.rb
112
113
  - spec/gyoku_spec.rb
113
114
  - spec/spec_helper.rb
114
- has_rdoc: true
115
115
  homepage: http://github.com/rubiii/gyoku
116
116
  licenses: []
117
117
 
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  requirements: []
142
142
 
143
143
  rubyforge_project: gyoku
144
- rubygems_version: 1.4.1
144
+ rubygems_version: 1.7.2
145
145
  signing_key:
146
146
  specification_version: 3
147
147
  summary: Converts Ruby Hashes to XML