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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e3d735bc992848923596d14a5a8538857a2d60f
4
- data.tar.gz: 57e7d9bae3d6782dc24a4bbb60e5b8d7f73dc089
3
+ metadata.gz: 13b8f36b7b0fbf447618133dfca642eed65bd43e
4
+ data.tar.gz: 74f77f34a0f65549ba932116050cb7a5ad3cf051
5
5
  SHA512:
6
- metadata.gz: a76520a35722177de93142da9b2f8ab286cfa872ceff5973dd840c3ba4e88763b2aed87c5e752f9b9b88f09a6ef5c57bf8eda75727d0153518e775ff29121643
7
- data.tar.gz: 34ca8dc2c753034119387014ccdd2ce4e7fccf50818154c3e43b6da656bc697c6b1c9f1f6c68f2336e0bb2d2e6966419ba081df0bf1b22813d5c66cb07d6d576
6
+ metadata.gz: a4464ca9e188003c44b77c40700da2b89f83c65141b22db3a9fff009509008dc8cddec709d0f05a2f6bfb6dc511ac64a6517f54a018c40f1454246e91b2c0beb
7
+ data.tar.gz: 63f77e214d1db43125511d44a766b96fc8b373e98fcdae3ea1201361c53c9227a72b6cdfe18d3efd01246082fd3df4b22cdeaefd60de9017170d5763b7c3ab53
@@ -1,9 +1,9 @@
1
1
  language: "ruby"
2
2
  script: "bundle exec rake"
3
3
  rvm:
4
- - 1.9.2
5
4
  - 1.9.3
6
5
  - 2.0.0
7
6
  - 2.1
7
+ - 2.2
8
8
  - jruby-19mode
9
9
  - rbx-2
@@ -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?
@@ -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
- iterate_with_xml array, attributes do |xml, item, attrs, index|
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 xml.tag!(key, attrs) { xml << Hash.to_xml(item, options) }
22
- when ::Array then xml.tag!(key, attrs) { xml << Array.to_xml(item, NESTED_ELEMENT_NAME) }
23
- when NilClass then xml.tag!(key, "xsi:nil" => "true")
24
- else xml.tag!(key, attrs) { xml << XMLValue.create(item, escape_xml) }
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)
@@ -1,3 +1,3 @@
1
1
  module Gyoku
2
- VERSION = '1.2.2'
2
+ VERSION = '1.2.3'
3
3
  end
@@ -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]
@@ -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>"
@@ -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.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: 2014-09-23 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder