Skip to content

Commit

Permalink
Merge pull request #53 from rlburkes/array-of-hashes-works
Browse files Browse the repository at this point in the history
Support serializing arrays of 'gyoku' hashes.
  • Loading branch information
tjarratt committed Apr 5, 2015
2 parents c9bed16 + 794819d commit 37d6fc9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/gyoku/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def self.to_xml(hash, options = {})
xml_key = XMLKey.create key, options

case
when :content! === key then xml << XMLValue.create(value, escape_xml)
when :content! === key then xml << XMLValue.create(value, escape_xml, options)
when ::Array === value then xml << Array.to_xml(value, xml_key, escape_xml, attributes, options.merge(:self_closing => self_closing))
when ::Hash === value then xml.tag!(xml_key, attributes) { xml << Hash.to_xml(value, options) }
when self_closing then xml.tag!(xml_key, attributes)
when NilClass === value then xml.tag!(xml_key, "xsi:nil" => "true")
else xml.tag!(xml_key, attributes) { xml << XMLValue.create(value, escape_xml) }
else xml.tag!(xml_key, attributes) { xml << XMLValue.create(value, escape_xml, options) }
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/gyoku/xml_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class << self
XS_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%Z"

# Converts a given +object+ to an XML value.
def create(object, escape_xml = true)
def create(object, escape_xml = true, options = {})
if Time === object
object.strftime XS_TIME_FORMAT
elsif DateTime === object
Expand All @@ -28,6 +28,8 @@ def create(object, escape_xml = true)
create object.to_datetime
elsif object.respond_to?(:call)
create object.call
elsif ::Hash === object
Gyoku::Hash.to_xml(object, options)
else
object.to_s
end
Expand Down
5 changes: 5 additions & 0 deletions spec/gyoku/xml_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def singleton.to_datetime
expect(create(object)).to eq("2012-03-22T16:22:33+00:00")
end

it "hash objects get converted to xml" do
object = { document!: { "@version" => "2.0", content!: { key!: "value", other_key: { "@attribute" => 'value', content!: { key: "value" } } } } }
expect(create(object)).to eq("<document version=\"2.0\"><key>value</key><otherKey attribute=\"value\"><key>value</key></otherKey></document>")
end

it "calls #to_s unless the Object responds to #to_datetime" do
expect(create("value")).to eq("value")
end
Expand Down

0 comments on commit 37d6fc9

Please sign in to comment.