lib/xommelier/xml/element/serialization.rb in xommelier-0.1.19 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.20
- old
+ new
@@ -114,23 +114,24 @@
alias_method :to_xommelier, :to_xml
def to_hash
attributes.dup.tap do |hash|
@elements.each do |name, value|
- options = element_options(name)
- type = options.type
- value = Array.wrap(value)
- if type < Xml::Element
- value = value.map(&:to_hash)
- end
- if value.count > 1
- name = name.to_s.pluralize.to_sym
+ element = element_options(name)
+ if element.multiple?
+ if value.count > 1
+ name = element.plural
+ value = value.map { |v| v.to_hash } if element.complex_type?
+ else
+ value = value.first.to_hash
+ end
else
- value = value.first
+ value = value.to_hash if element.complex_type?
end
hash[name] = value
end
+ hash[:text] = text if text?
end
end
def <=>(other)
if text? && other.is_a?(String)
@@ -175,11 +176,11 @@
def children_namespaces(namespaces = Set[xmlns])
elements.inject(namespaces) do |result, (name, children)|
element = self.class.elements[name]
result << element.ns
result += attributes.keys.map { |attr_name| attribute_options(attr_name).ns }
- if element.type < Xml::Element
+ if element.complex_type?
Array(children).each do |child|
result += child.children_namespaces
end
end
result
@@ -222,10 +223,10 @@
end
# @param [Nokogiri::XML::Node] node
# @param [Xommelier::Xml::Element::Structure::Element] options
def typecast_element(node, options)
- if options.type < Xml::Element
+ if options.complex_type?
options.type.from_xommelier(xml_document, node: node)
else
options.type.from_xommelier(node.text)
end
end