gyoku 0.2.0 → 0.3.0

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.
data/README.md CHANGED
@@ -25,6 +25,7 @@ Conventions
25
25
 
26
26
  * Hash key Symbols are converted to lowerCamelCase Strings
27
27
  * Hash key Strings are not converted and may contain namespaces
28
+ * Hash keys ending with a forward slash create self-closing tags
28
29
  * DateTime values are converted to xs:dateTime Strings
29
30
  * Objects responding to :to_datetime (except Strings) are converted to xs:dateTime Strings
30
31
  * TrueClass and FalseClass objects are converted to "true" and "false" Strings
data/lib/gyoku.rb CHANGED
@@ -3,12 +3,9 @@ require "gyoku/hash"
3
3
 
4
4
  module Gyoku
5
5
 
6
- def self.xml(object)
7
- case object
8
- when ::Hash then Hash.to_xml object
9
- when ::Array then Array.to_xml object
10
- else raise ArgumentError, "Expected kind_of Array || Hash"
11
- end
6
+ # Translates a given +hash+ with +options+ to XML.
7
+ def self.xml(hash, options = {})
8
+ Hash.to_xml hash, options
12
9
  end
13
10
 
14
11
  end
data/lib/gyoku/array.rb CHANGED
@@ -37,7 +37,7 @@ module Gyoku
37
37
  # for duplicate tags.
38
38
  def self.tag_attributes(attributes, index)
39
39
  return {} if attributes.empty?
40
-
40
+
41
41
  attributes.inject({}) do |hash, (key, value)|
42
42
  value = value[index] if value.kind_of? ::Array
43
43
  hash.merge key => value
data/lib/gyoku/hash.rb CHANGED
@@ -9,21 +9,19 @@ module Gyoku
9
9
  extend XMLKey
10
10
  extend XMLValue
11
11
 
12
- # Translates a given +hash+ to XML.
13
- def self.to_xml(hash)
14
- iterate_with_xml hash do |xml, key, attributes|
15
- attrs = attributes[key] || {}
16
- value = hash[key]
12
+ # Translates a given +hash+ with +options+ to XML.
13
+ def self.to_xml(hash, options = {})
14
+ iterate_with_xml hash do |xml, key, value, attributes|
17
15
  self_closing = key.to_s[-1, 1] == "/"
18
16
  escape_xml = key.to_s[-1, 1] != "!"
19
- key = to_xml_key(key)
20
-
17
+ xml_key = to_xml_key key, options
18
+
21
19
  case
22
- when ::Array === value then xml << Array.to_xml(value, key, escape_xml, attrs)
23
- when ::Hash === value then xml.tag!(key, attrs) { xml << Hash.to_xml(value) }
24
- when self_closing then xml.tag!(key, attrs)
25
- when NilClass === value then xml.tag!(key, "xsi:nil" => "true")
26
- else xml.tag!(key, attrs) { xml << to_xml_value(value, escape_xml) }
20
+ when ::Array === value then xml << Array.to_xml(value, xml_key, escape_xml, attributes)
21
+ when ::Hash === value then xml.tag!(xml_key, attributes) { xml << Hash.to_xml(value, options) }
22
+ when self_closing then xml.tag!(xml_key, attributes)
23
+ when NilClass === value then xml.tag!(xml_key, "xsi:nil" => "true")
24
+ else xml.tag!(xml_key, attributes) { xml << to_xml_value(value, escape_xml) }
27
25
  end
28
26
  end
29
27
  end
@@ -35,9 +33,9 @@ module Gyoku
35
33
  def self.iterate_with_xml(hash)
36
34
  xml = Builder::XmlMarkup.new
37
35
  attributes = hash.delete(:attributes!) || {}
38
-
39
- order(hash).each { |key| yield xml, key, attributes }
40
-
36
+
37
+ order(hash).each { |key| yield xml, key, hash[key], (attributes[key] || {}) }
38
+
41
39
  xml.target!
42
40
  end
43
41
 
@@ -47,11 +45,11 @@ module Gyoku
47
45
  def self.order(hash)
48
46
  order = hash.delete :order!
49
47
  order = hash.keys unless order.kind_of? ::Array
50
-
48
+
51
49
  missing, spurious = hash.keys - order, order - hash.keys
52
50
  raise ArgumentError, "Missing elements in :order! #{missing.inspect}" unless missing.empty?
53
51
  raise ArgumentError, "Spurious elements in :order! #{spurious.inspect}" unless spurious.empty?
54
-
52
+
55
53
  order
56
54
  end
57
55
 
data/lib/gyoku/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Gyoku
2
2
 
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
 
5
5
  end
data/lib/gyoku/xml_key.rb CHANGED
@@ -3,11 +3,19 @@ require "gyoku/core_ext/string"
3
3
  module Gyoku
4
4
  module XMLKey
5
5
 
6
- # Converts a given +object+ to an XML key.
7
- def to_xml_key(key)
6
+ # Converts a given +object+ with +options+ to an XML key.
7
+ def to_xml_key(key, options = {})
8
+ xml_key = chop_special_characters key.to_s
9
+
10
+ if unqualify?(xml_key)
11
+ xml_key = xml_key.split(":").last
12
+ elsif qualify?(options) && !xml_key.include?(":")
13
+ xml_key = "#{options[:namespace]}:#{xml_key}"
14
+ end
15
+
8
16
  case key
9
- when Symbol then chop_special_characters(key.to_s).lower_camelcase
10
- else chop_special_characters(key.to_s)
17
+ when Symbol then xml_key.lower_camelcase
18
+ else xml_key
11
19
  end
12
20
  end
13
21
 
@@ -18,5 +26,15 @@ module Gyoku
18
26
  ["!", "/"].include?(string[-1, 1]) ? string.chop : string
19
27
  end
20
28
 
29
+ # Returns whether to remove the namespace from a given +key+.
30
+ def unqualify?(key)
31
+ key[0, 1] == ":"
32
+ end
33
+
34
+ # Returns whether to namespace all keys (elementFormDefault).
35
+ def qualify?(options)
36
+ options[:element_form_default] == :qualified && options[:namespace]
37
+ end
38
+
21
39
  end
22
40
  end
@@ -47,14 +47,14 @@ describe Gyoku::Hash do
47
47
  def singleton.to_datetime
48
48
  DateTime.new(2012, 03, 22, 16, 22, 33)
49
49
  end
50
-
50
+
51
51
  to_xml(:before => singleton).should == "<before>2012-03-22T16:22:33+00:00</before>"
52
52
  end
53
53
 
54
54
  it "should call 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
 
@@ -115,15 +115,28 @@ describe Gyoku::Hash do
115
115
  hash = { :find_user => { :person => ["Lucy", "Anna"], :attributes! => { :person => { :id => [1, 3] } } } }
116
116
  result = '<findUser><person id="1">Lucy</person><person id="3">Anna</person></findUser>'
117
117
  to_xml(hash).should == result
118
-
118
+
119
119
  hash = { :find_user => { :person => ["Lucy", "Anna"], :attributes! => { :person => { :active => "true" } } } }
120
120
  result = '<findUser><person active="true">Lucy</person><person active="true">Anna</person></findUser>'
121
121
  to_xml(hash).should == result
122
122
  end
123
+
124
+ 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" } }
127
+ result = to_xml hash, :element_form_default => :qualified, :namespace => :v1
128
+
129
+ result.should include(
130
+ "<v1:first><v1:first_name>Luvy</v1:first_name></v1:first>",
131
+ "<second><firstName>Anna</firstName></second>",
132
+ "<v2:third><v2:firstName>Danie</v2:firstName></v2:third>"
133
+ )
134
+ end
135
+ end
123
136
  end
124
137
 
125
- def to_xml(hash)
126
- Gyoku::Hash.to_xml hash
138
+ def to_xml(hash, options = {})
139
+ Gyoku::Hash.to_xml hash, options
127
140
  end
128
141
 
129
142
  end
@@ -20,6 +20,18 @@ describe Gyoku::XMLKey do
20
20
  to_xml_key(:lower_camel_case).should == "lowerCamelCase"
21
21
  to_xml_key(:lower_camel_case!).should == "lowerCamelCase"
22
22
  end
23
+
24
+ context "with :element_form_default set to :qualified and a :namespace" do
25
+ it "should add the given namespace" do
26
+ key = to_xml_key :qualify, :element_form_default => :qualified, :namespace => :v1
27
+ key.should == "v1:qualify"
28
+ end
29
+
30
+ it "should not add the given namespace if the key starts with a colon" do
31
+ key = to_xml_key ":qualify", :element_form_default => :qualified, :namespace => :v1
32
+ key.should == "qualify"
33
+ end
34
+ end
23
35
  end
24
36
 
25
37
  end
data/spec/gyoku_spec.rb CHANGED
@@ -4,13 +4,8 @@ describe Gyoku do
4
4
 
5
5
  describe ".xml" do
6
6
  it "should translate a given Hash to XML" do
7
- Gyoku::Hash.expects(:to_xml).with(:id => 1)
8
- Gyoku.xml :id => 1
9
- end
10
-
11
- it "should translate a given Array to XML" do
12
- Gyoku::Array.expects(:to_xml).with([1, 2, 3])
13
- Gyoku.xml [1, 2, 3]
7
+ Gyoku::Hash.expects(:to_xml).with({:id => 1}, :element_form_default => :qualified)
8
+ Gyoku.xml({ :id => 1 }, :element_form_default => :qualified)
14
9
  end
15
10
  end
16
11
 
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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Harrington
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-08 00:00:00 +01:00
18
+ date: 2011-01-23 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency