gyoku 1.2.2 → 1.2.3
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 +4 -4
- data/.travis.yml +1 -1
- data/CHANGELOG.md +4 -0
- data/lib/gyoku/array.rb +34 -8
- data/lib/gyoku/version.rb +1 -1
- data/lib/gyoku/xml_key.rb +2 -0
- data/spec/gyoku/array_spec.rb +7 -0
- data/spec/gyoku/xml_key_spec.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b8f36b7b0fbf447618133dfca642eed65bd43e
|
4
|
+
data.tar.gz: 74f77f34a0f65549ba932116050cb7a5ad3cf051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4464ca9e188003c44b77c40700da2b89f83c65141b22db3a9fff009509008dc8cddec709d0f05a2f6bfb6dc511ac64a6517f54a018c40f1454246e91b2c0beb
|
7
|
+
data.tar.gz: 63f77e214d1db43125511d44a766b96fc8b373e98fcdae3ea1201361c53c9227a72b6cdfe18d3efd01246082fd3df4b22cdeaefd60de9017170d5763b7c3ab53
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 1.2.3 (2015-03-10)
|
2
|
+
|
3
|
+
# Feature: [#52](https://github.com/savonrb/gyoku/pull/52) Adds an :unwrap option that allows an array of hashes to be unwrapped into a single array xml node, rather than one per hash.
|
4
|
+
|
1
5
|
## 1.2.2 (2014-09-22)
|
2
6
|
|
3
7
|
* Fixed a bug introduced by making Gyoku threadsafe. Who knew that `$1` and the block variable that `#gsub` provides are not the same?
|
data/lib/gyoku/array.rb
CHANGED
@@ -11,17 +11,27 @@ module Gyoku
|
|
11
11
|
# Translates a given +array+ to XML. Accepts the XML +key+ to add the elements to,
|
12
12
|
# whether to +escape_xml+ and an optional Hash of +attributes+.
|
13
13
|
def self.to_xml(array, key, escape_xml = true, attributes = {}, options = {})
|
14
|
+
|
14
15
|
self_closing = options.delete(:self_closing)
|
15
|
-
|
16
|
+
unwrap = options[:unwrap] || false
|
17
|
+
|
18
|
+
iterate_with_xml array, key, attributes, options do |xml, item, attrs, index|
|
16
19
|
if self_closing
|
17
20
|
xml.tag!(key, attrs)
|
18
|
-
|
19
21
|
else
|
20
22
|
case item
|
21
|
-
when ::Hash then
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
when ::Hash then
|
24
|
+
if unwrap
|
25
|
+
xml << Hash.to_xml(item, options)
|
26
|
+
else
|
27
|
+
xml.tag!(key, attrs) { xml << Hash.to_xml(item, options) }
|
28
|
+
end
|
29
|
+
when ::Array then
|
30
|
+
xml.tag!(key, attrs) { xml << Array.to_xml(item, NESTED_ELEMENT_NAME) }
|
31
|
+
when NilClass then
|
32
|
+
xml.tag!(key, "xsi:nil" => "true")
|
33
|
+
else
|
34
|
+
xml.tag!(key, attrs) { xml << XMLValue.create(item, escape_xml) }
|
25
35
|
end
|
26
36
|
end
|
27
37
|
end
|
@@ -31,8 +41,24 @@ module Gyoku
|
|
31
41
|
|
32
42
|
# Iterates over a given +array+ with a Hash of +attributes+ and yields a builder +xml+
|
33
43
|
# instance, the current +item+, any XML +attributes+ and the current +index+.
|
34
|
-
def self.iterate_with_xml(array, attributes)
|
44
|
+
def self.iterate_with_xml(array, key, attributes, options, &block)
|
45
|
+
|
35
46
|
xml = Builder::XmlMarkup.new
|
47
|
+
unwrap = options[:unwrap] || false
|
48
|
+
|
49
|
+
if (unwrap)
|
50
|
+
xml.tag!(key) { iterate_array(xml, array, attributes, &block) }
|
51
|
+
else
|
52
|
+
iterate_array(xml, array, attributes, &block)
|
53
|
+
end
|
54
|
+
|
55
|
+
xml.target!
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# Iterates over a given +array+ with a Hash of +attributes+ and yields a builder +xml+
|
60
|
+
# instance, the current +item+, any XML +attributes+ and the current +index+.
|
61
|
+
def self.iterate_array(xml, array, attributes, &block)
|
36
62
|
array.each_with_index do |item, index|
|
37
63
|
if item.respond_to?(:keys)
|
38
64
|
attrs = item.reduce({}) do |st, v|
|
@@ -45,9 +71,9 @@ module Gyoku
|
|
45
71
|
end
|
46
72
|
yield xml, item, tag_attributes(attributes, index).merge(attrs), index
|
47
73
|
end
|
48
|
-
xml.target!
|
49
74
|
end
|
50
75
|
|
76
|
+
|
51
77
|
# Takes a Hash of +attributes+ and the +index+ for which to return attributes
|
52
78
|
# for duplicate tags.
|
53
79
|
def self.tag_attributes(attributes, index)
|
data/lib/gyoku/version.rb
CHANGED
data/lib/gyoku/xml_key.rb
CHANGED
@@ -34,6 +34,8 @@ module Gyoku
|
|
34
34
|
|
35
35
|
# Returns the formula for converting Symbol keys.
|
36
36
|
def key_converter(options, xml_key)
|
37
|
+
return options[:key_converter] if options[:key_converter].is_a? Proc
|
38
|
+
|
37
39
|
defined_key = options[:key_to_convert]
|
38
40
|
if (defined_key != nil) && (defined_key == xml_key)
|
39
41
|
key_converter = options[:key_converter]
|
data/spec/gyoku/array_spec.rb
CHANGED
@@ -10,6 +10,13 @@ describe Gyoku::Array do
|
|
10
10
|
expect(to_xml(array, "user")).to eq(result)
|
11
11
|
end
|
12
12
|
|
13
|
+
it "returns the XML for an Array of Hashes unwrapped" do
|
14
|
+
array = [{ :name => "adam" }, { :name => "eve" }]
|
15
|
+
result = "<user><name>adam</name><name>eve</name></user>"
|
16
|
+
|
17
|
+
expect(to_xml(array, "user", true, {}, :unwrap => true)).to eq(result)
|
18
|
+
end
|
19
|
+
|
13
20
|
it "returns the XML for an Array of different Objects" do
|
14
21
|
array = [:symbol, "string", 123]
|
15
22
|
result = "<value>symbol</value><value>string</value><value>123</value>"
|
data/spec/gyoku/xml_key_spec.rb
CHANGED
@@ -20,7 +20,19 @@ describe Gyoku::XMLKey do
|
|
20
20
|
expect(create(:lower_camel_case!)).to eq("lowerCamelCase")
|
21
21
|
end
|
22
22
|
|
23
|
+
context "when the converter option is set to camelcase" do
|
24
|
+
it "should replace / with ::, and turn snake case into camel case" do
|
25
|
+
input = "hello_world_bob/how_are_you|there:foo^bar".to_sym
|
26
|
+
expected_output = "HelloWorldBob::HowAreYou|there:foo^bar"
|
27
|
+
expect(create(input, {key_converter: :camelcase})).to eq(expected_output)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
23
31
|
context "with key_converter" do
|
32
|
+
it "accepts lambda converters" do
|
33
|
+
expect(create(:some_text, {key_converter: lambda { |k| k.reverse }})).to eq("txet_emos")
|
34
|
+
end
|
35
|
+
|
24
36
|
it "convert symbol to the specified type" do
|
25
37
|
expect(create(:some_text, {key_converter: :camelcase})).to eq("SomeText")
|
26
38
|
expect(create(:some_text, {key_converter: :upcase})).to eq("SOME_TEXT")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gyoku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Harrington
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: builder
|