gyoku 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +7 -3
- data/CHANGELOG.md +12 -0
- data/README.md +68 -40
- data/Rakefile +1 -0
- data/gyoku.gemspec +2 -3
- data/lib/gyoku/array.rb +1 -1
- data/lib/gyoku/hash.rb +9 -5
- data/lib/gyoku/version.rb +1 -1
- data/lib/gyoku/xml_key.rb +5 -4
- data/spec/gyoku/array_spec.rb +7 -0
- data/spec/gyoku/hash_spec.rb +39 -3
- metadata +23 -43
- data/.gemtest +0 -0
- data/lib/gyoku/core_ext/string.rb +0 -19
- data/spec/gyoku/core_ext/string_spec.rb +0 -17
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## 0.4.5 (2012-05-28)
|
2
|
+
|
3
|
+
* Fix: [issue 8](https://github.com/rubiii/gyoku/issues/8) -
|
4
|
+
Conflict between camelcase methods in Rails.
|
5
|
+
|
6
|
+
* Fix: [pull request 15](https://github.com/rubiii/gyoku/pull/15) -
|
7
|
+
Gyoku generates blank attribute values if there are fewer attribute
|
8
|
+
values in attributes! than elements.
|
9
|
+
|
10
|
+
* Fix: [issue 12](https://github.com/rubiii/gyoku/issues/12) -
|
11
|
+
Don't remove special keys from the original Hash.
|
12
|
+
|
1
13
|
## 0.4.4
|
2
14
|
|
3
15
|
* Fix: [issue 6](https://github.com/rubiii/gyoku/issues/6) -
|
data/README.md
CHANGED
@@ -1,34 +1,61 @@
|
|
1
|
-
Gyoku ![
|
1
|
+
Gyoku [![Build Status](https://secure.travis-ci.org/rubiii/gyoku.png)](http://travis-ci.org/rubiii/gyoku)
|
2
2
|
=====
|
3
3
|
|
4
|
-
|
4
|
+
##### Translates Ruby Hashes to XML
|
5
5
|
|
6
|
-
|
6
|
+
Gyoku is available through [Rubygems](http://rubygems.org/gems/gyoku) and can
|
7
|
+
be installed via:
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
```
|
10
|
+
$ gem install gyoku
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
Gyoku is based on a few conventions.
|
12
14
|
|
13
|
-
|
15
|
+
``` ruby
|
16
|
+
Gyoku.xml(:find_user => { :id => 123, "v1:Key" => "api" })
|
17
|
+
# => "<findUser><id>123</id><v1:Key>api</v1:Key></findUser>"
|
18
|
+
```
|
14
19
|
|
15
|
-
An example
|
16
|
-
----------
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
Hash keys
|
22
|
+
---------
|
20
23
|
|
21
|
-
|
24
|
+
Hash key Symbols are converted to lowerCamelCase Strings.
|
22
25
|
|
23
|
-
|
24
|
-
|
26
|
+
``` ruby
|
27
|
+
Gyoku.xml(:lower_camel_case => "key")
|
28
|
+
# => "<lowerCamelCase>key</lowerCamelCase>"
|
29
|
+
```
|
30
|
+
|
31
|
+
You can change the default conversion formula.
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
Gyoku.convert_symbols_to :camelcase # or one of [:none, :lower_camelcase]
|
35
|
+
|
36
|
+
Gyoku.xml(:camel_case => "key")
|
37
|
+
# => "<CamelCase>key</CamelCase>"
|
38
|
+
```
|
39
|
+
|
40
|
+
And you can also define your own formula.
|
41
|
+
|
42
|
+
``` ruby
|
43
|
+
Gyoku.convert_symbols_to { |key| key.upcase }
|
44
|
+
|
45
|
+
Gyoku.xml(:upcase => "key")
|
46
|
+
# => "<UPCASE>key</UPCASE>"
|
47
|
+
```
|
48
|
+
|
49
|
+
Hash key Strings are not converted and may contain namespaces.
|
25
50
|
|
26
|
-
|
51
|
+
``` ruby
|
52
|
+
Gyoku.xml("XML" => "key")
|
53
|
+
# => "<XML>key</XML>"
|
54
|
+
```
|
27
55
|
|
28
|
-
* Symbols are converted to lowerCamelCase Strings
|
29
|
-
* Strings are not converted and may contain namespaces
|
30
56
|
|
31
|
-
|
57
|
+
Hash values
|
58
|
+
-----------
|
32
59
|
|
33
60
|
* DateTime objects are converted to xs:dateTime Strings
|
34
61
|
* Objects responding to :to_datetime (except Strings) are converted to xs:dateTime Strings
|
@@ -37,45 +64,46 @@ Conventions
|
|
37
64
|
* These conventions are also applied to the return value of objects responding to :call
|
38
65
|
* All other objects are converted to Strings using :to_s
|
39
66
|
|
40
|
-
Symbols are converted to lowerCamelCase?
|
41
|
-
----------------------------------------
|
42
|
-
|
43
|
-
That's the default. But you can use one of the other conversion formulas:
|
44
|
-
|
45
|
-
Gyoku.convert_symbols_to :camelcase # or one of [:none, :lower_camelcase]
|
46
|
-
|
47
|
-
or even define you own one:
|
48
|
-
|
49
|
-
Gyoku.convert_symbols_to { |key| key.upcase }
|
50
67
|
|
51
68
|
Special characters
|
52
69
|
------------------
|
53
70
|
|
54
|
-
Gyoku escapes special characters unless the Hash key ends with an exclamation mark
|
71
|
+
Gyoku escapes special characters unless the Hash key ends with an exclamation mark.
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
Gyoku.xml(:escaped => "<tag />", :not_escaped! => "<tag />")
|
75
|
+
# => "<escaped><tag /></escaped><notEscaped><tag /></notEscaped>"
|
76
|
+
```
|
55
77
|
|
56
|
-
Gyoku.xml :escaped => "<tag />", :not_escaped! => "<tag />"
|
57
|
-
# => "<escaped><tag /></escaped><notEscaped><tag /></notEscaped>"
|
58
78
|
|
59
79
|
Self-closing tags
|
60
80
|
-----------------
|
61
81
|
|
62
|
-
Hash Keys ending with a forward slash create self-closing tags
|
82
|
+
Hash Keys ending with a forward slash create self-closing tags.
|
83
|
+
|
84
|
+
``` ruby
|
85
|
+
Gyoku.xml(:"self_closing/" => "", "selfClosing/" => nil)
|
86
|
+
# => "<selfClosing/><selfClosing/>"
|
87
|
+
```
|
63
88
|
|
64
|
-
Gyoku.xml :"self_closing/" => "", "selfClosing/" => nil
|
65
|
-
# => "<selfClosing/><selfClosing/>"
|
66
89
|
|
67
90
|
Sort XML tags
|
68
91
|
-------------
|
69
92
|
|
70
|
-
In case you need the XML tags to be in a specific order, you can specify the order through an additional Array stored under an
|
93
|
+
In case you need the XML tags to be in a specific order, you can specify the order through an additional Array stored under an `:order!` key.
|
94
|
+
|
95
|
+
``` ruby
|
96
|
+
Gyoku.xml(:name => "Eve", :id => 1, :order! => [:id, :name])
|
97
|
+
# => "<id>1</id><name>Eve</name>"
|
98
|
+
```
|
71
99
|
|
72
|
-
Gyoku.xml :name => "Eve", :id => 1, :order! => [:id, :name]
|
73
|
-
# => "<id>1</id><name>Eve</name>"
|
74
100
|
|
75
101
|
XML attributes
|
76
102
|
--------------
|
77
103
|
|
78
|
-
Adding XML attributes is rather ugly, but it can be done by specifying an additional Hash stored under an
|
104
|
+
Adding XML attributes is rather ugly, but it can be done by specifying an additional Hash stored under an `:attributes!` key.
|
79
105
|
|
80
|
-
|
81
|
-
|
106
|
+
``` ruby
|
107
|
+
Gyoku.xml(:person => "Eve", :attributes! => { :person => { :id => 1 } })
|
108
|
+
# => "<person id=\"1\">Eve</person>"
|
109
|
+
```
|
data/Rakefile
CHANGED
data/gyoku.gemspec
CHANGED
@@ -15,9 +15,8 @@ Gem::Specification.new do |s|
|
|
15
15
|
|
16
16
|
s.add_dependency "builder", ">= 2.1.2"
|
17
17
|
|
18
|
-
s.add_development_dependency "
|
19
|
-
s.add_development_dependency "
|
20
|
-
s.add_development_dependency "mocha", "~> 0.9.9"
|
18
|
+
s.add_development_dependency "rake", "~> 0.9"
|
19
|
+
s.add_development_dependency "rspec", "~> 2.10"
|
21
20
|
|
22
21
|
s.files = `git ls-files`.split("\n")
|
23
22
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/gyoku/array.rb
CHANGED
data/lib/gyoku/hash.rb
CHANGED
@@ -30,9 +30,12 @@ module Gyoku
|
|
30
30
|
# Hash +key+ and any XML +attributes+.
|
31
31
|
def self.iterate_with_xml(hash)
|
32
32
|
xml = Builder::XmlMarkup.new
|
33
|
-
attributes = hash
|
33
|
+
attributes = hash[:attributes!] || {}
|
34
|
+
hash_without_attributes = hash.reject { |key, value| key == :attributes! }
|
34
35
|
|
35
|
-
order(
|
36
|
+
order(hash_without_attributes).each do |key|
|
37
|
+
yield xml, key, hash_without_attributes[key], (attributes[key] || {})
|
38
|
+
end
|
36
39
|
|
37
40
|
xml.target!
|
38
41
|
end
|
@@ -41,10 +44,11 @@ module Gyoku
|
|
41
44
|
# Defaults to return the actual keys of the Hash if no :order! key could be found.
|
42
45
|
# Raises an ArgumentError in case the :order! Array does not match the Hash keys.
|
43
46
|
def self.order(hash)
|
44
|
-
order = hash
|
45
|
-
|
47
|
+
order = hash[:order!]
|
48
|
+
hash_without_order = hash.reject { |key, value| key == :order! }
|
49
|
+
order = hash_without_order.keys unless order.kind_of? ::Array
|
46
50
|
|
47
|
-
missing, spurious =
|
51
|
+
missing, spurious = hash_without_order.keys - order, order - hash_without_order.keys
|
48
52
|
raise ArgumentError, "Missing elements in :order! #{missing.inspect}" unless missing.empty?
|
49
53
|
raise ArgumentError, "Spurious elements in :order! #{spurious.inspect}" unless spurious.empty?
|
50
54
|
|
data/lib/gyoku/version.rb
CHANGED
data/lib/gyoku/xml_key.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require "gyoku/core_ext/string"
|
2
|
-
|
3
1
|
module Gyoku
|
4
2
|
module XMLKey
|
5
3
|
class << self
|
6
4
|
|
5
|
+
CAMELCASE = lambda { |key| key.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } }
|
6
|
+
LOWER_CAMELCASE = lambda { |key| key[0].chr.downcase + CAMELCASE.call(key)[1..-1] }
|
7
|
+
|
7
8
|
FORMULAS = {
|
8
|
-
:lower_camelcase => lambda { |key| key
|
9
|
-
:camelcase => lambda { |key| key
|
9
|
+
:lower_camelcase => lambda { |key| LOWER_CAMELCASE.call(key) },
|
10
|
+
:camelcase => lambda { |key| CAMELCASE.call(key) },
|
10
11
|
:none => lambda { |key| key }
|
11
12
|
}
|
12
13
|
|
data/spec/gyoku/array_spec.rb
CHANGED
@@ -44,6 +44,13 @@ describe Gyoku::Array do
|
|
44
44
|
|
45
45
|
to_xml(array, "value", :escape_xml, :id => [1, 2]).should == result
|
46
46
|
end
|
47
|
+
|
48
|
+
it "skips attribute for element without attributes if there are fewer attributes than elements" do
|
49
|
+
array = ["adam", "eve", "serpent"]
|
50
|
+
result = '<value id="1">adam</value><value id="2">eve</value><value>serpent</value>'
|
51
|
+
|
52
|
+
to_xml(array, "value", :escape_xml, :id => [1, 2]).should == result
|
53
|
+
end
|
47
54
|
end
|
48
55
|
|
49
56
|
def to_xml(*args)
|
data/spec/gyoku/hash_spec.rb
CHANGED
@@ -52,10 +52,12 @@ describe Gyoku::Hash do
|
|
52
52
|
end
|
53
53
|
|
54
54
|
it "calls to_s on Strings even if they respond to to_datetime" do
|
55
|
-
|
56
|
-
|
55
|
+
singleton = "gorilla"
|
56
|
+
def singleton.to_datetime
|
57
|
+
DateTime.new(2012, 03, 22, 16, 22, 33)
|
58
|
+
end
|
57
59
|
|
58
|
-
to_xml(:name =>
|
60
|
+
to_xml(:name => singleton).should == "<name>gorilla</name>"
|
59
61
|
end
|
60
62
|
|
61
63
|
it "properly serializes nil values" do
|
@@ -123,6 +125,12 @@ describe Gyoku::Hash do
|
|
123
125
|
to_xml(hash).should == result
|
124
126
|
end
|
125
127
|
|
128
|
+
it "skips attribute for element without attributes if there are fewer attributes than elements" do
|
129
|
+
hash = { :find_user => { :person => ["Lucy", "Anna", "Beth"], :attributes! => { :person => { :id => [1, 3] } } } }
|
130
|
+
result = '<findUser><person id="1">Lucy</person><person id="3">Anna</person><person>Beth</person></findUser>'
|
131
|
+
to_xml(hash).should == result
|
132
|
+
end
|
133
|
+
|
126
134
|
it "adds attributes to self-closing tags" do
|
127
135
|
hash = {
|
128
136
|
"category/" => "",
|
@@ -151,6 +159,34 @@ describe Gyoku::Hash do
|
|
151
159
|
result.should include("<v1:array>", "<v1:first>Lucy</v1:first>", "<v1:second>Anna</v1:second>")
|
152
160
|
end
|
153
161
|
end
|
162
|
+
|
163
|
+
it "does not remove special keys from the original Hash" do
|
164
|
+
hash = {
|
165
|
+
:persons => {
|
166
|
+
:first => "Lucy",
|
167
|
+
:second => "Anna",
|
168
|
+
:order! => [:second, :first],
|
169
|
+
:attributes! => { :first => { :first => true } }
|
170
|
+
},
|
171
|
+
:countries => [:de, :us],
|
172
|
+
:order! => [:countries, :persons],
|
173
|
+
:attributes! => { :countries => { :array => true } }
|
174
|
+
}
|
175
|
+
|
176
|
+
to_xml(hash)
|
177
|
+
|
178
|
+
hash.should == {
|
179
|
+
:persons => {
|
180
|
+
:first => "Lucy",
|
181
|
+
:second => "Anna",
|
182
|
+
:order! => [:second, :first],
|
183
|
+
:attributes! => { :first => { :first => true } }
|
184
|
+
},
|
185
|
+
:countries => [:de, :us],
|
186
|
+
:order! => [:countries, :persons],
|
187
|
+
:attributes! => { :countries => { :array => true } }
|
188
|
+
}
|
189
|
+
end
|
154
190
|
end
|
155
191
|
|
156
192
|
def to_xml(hash, options = {})
|
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:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 5
|
10
|
+
version: 0.4.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel Harrington
|
@@ -15,12 +15,10 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-05-28 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
22
|
none: false
|
25
23
|
requirements:
|
26
24
|
- - ">="
|
@@ -31,54 +29,40 @@ dependencies:
|
|
31
29
|
- 1
|
32
30
|
- 2
|
33
31
|
version: 2.1.2
|
32
|
+
name: builder
|
34
33
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: rspec
|
38
34
|
prerelease: false
|
39
|
-
requirement:
|
35
|
+
requirement: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
38
|
none: false
|
41
39
|
requirements:
|
42
40
|
- - ~>
|
43
41
|
- !ruby/object:Gem::Version
|
44
|
-
hash:
|
42
|
+
hash: 25
|
45
43
|
segments:
|
46
|
-
- 2
|
47
|
-
- 4
|
48
44
|
- 0
|
49
|
-
|
45
|
+
- 9
|
46
|
+
version: "0.9"
|
47
|
+
name: rake
|
50
48
|
type: :development
|
51
|
-
version_requirements: *id002
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
|
-
name: autotest
|
54
49
|
prerelease: false
|
55
|
-
requirement:
|
56
|
-
none: false
|
57
|
-
requirements:
|
58
|
-
- - ">="
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
|
-
segments:
|
62
|
-
- 0
|
63
|
-
version: "0"
|
64
|
-
type: :development
|
65
|
-
version_requirements: *id003
|
50
|
+
requirement: *id002
|
66
51
|
- !ruby/object:Gem::Dependency
|
67
|
-
|
68
|
-
prerelease: false
|
69
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
70
53
|
none: false
|
71
54
|
requirements:
|
72
55
|
- - ~>
|
73
56
|
- !ruby/object:Gem::Version
|
74
|
-
hash:
|
57
|
+
hash: 23
|
75
58
|
segments:
|
76
|
-
-
|
77
|
-
-
|
78
|
-
|
79
|
-
|
59
|
+
- 2
|
60
|
+
- 10
|
61
|
+
version: "2.10"
|
62
|
+
name: rspec
|
80
63
|
type: :development
|
81
|
-
|
64
|
+
prerelease: false
|
65
|
+
requirement: *id003
|
82
66
|
description: Gyoku converts Ruby Hashes to XML
|
83
67
|
email: me@rubiii.com
|
84
68
|
executables: []
|
@@ -88,7 +72,6 @@ extensions: []
|
|
88
72
|
extra_rdoc_files: []
|
89
73
|
|
90
74
|
files:
|
91
|
-
- .gemtest
|
92
75
|
- .gitignore
|
93
76
|
- .rspec
|
94
77
|
- .travis.yml
|
@@ -100,13 +83,11 @@ files:
|
|
100
83
|
- gyoku.gemspec
|
101
84
|
- lib/gyoku.rb
|
102
85
|
- lib/gyoku/array.rb
|
103
|
-
- lib/gyoku/core_ext/string.rb
|
104
86
|
- lib/gyoku/hash.rb
|
105
87
|
- lib/gyoku/version.rb
|
106
88
|
- lib/gyoku/xml_key.rb
|
107
89
|
- lib/gyoku/xml_value.rb
|
108
90
|
- spec/gyoku/array_spec.rb
|
109
|
-
- spec/gyoku/core_ext/string_spec.rb
|
110
91
|
- spec/gyoku/hash_spec.rb
|
111
92
|
- spec/gyoku/xml_key_spec.rb
|
112
93
|
- spec/gyoku/xml_value_spec.rb
|
@@ -141,13 +122,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
122
|
requirements: []
|
142
123
|
|
143
124
|
rubyforge_project: gyoku
|
144
|
-
rubygems_version: 1.
|
125
|
+
rubygems_version: 1.8.21
|
145
126
|
signing_key:
|
146
127
|
specification_version: 3
|
147
128
|
summary: Converts Ruby Hashes to XML
|
148
129
|
test_files:
|
149
130
|
- spec/gyoku/array_spec.rb
|
150
|
-
- spec/gyoku/core_ext/string_spec.rb
|
151
131
|
- spec/gyoku/hash_spec.rb
|
152
132
|
- spec/gyoku/xml_key_spec.rb
|
153
133
|
- spec/gyoku/xml_value_spec.rb
|
data/.gemtest
DELETED
File without changes
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Gyoku
|
2
|
-
module CoreExt
|
3
|
-
module String
|
4
|
-
|
5
|
-
# Returns the String in lowerCamelCase.
|
6
|
-
def lower_camelcase
|
7
|
-
self[0].chr.downcase + self.camelcase[1..-1]
|
8
|
-
end
|
9
|
-
|
10
|
-
# Returns the String in CamelCase.
|
11
|
-
def camelcase
|
12
|
-
self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
String.send :include, Gyoku::CoreExt::String
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe String do
|
4
|
-
|
5
|
-
describe "#lower_camelcase" do
|
6
|
-
it "converts a snakecase String to lowerCamelCase" do
|
7
|
-
"lower_camel_case".lower_camelcase.should == "lowerCamelCase"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#camelcase" do
|
12
|
-
it "converts a snakecase String to CamelCase" do
|
13
|
-
"camel_case".camelcase.should == "CamelCase"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|