Module: Xommelier::Xml::Element::Serialization

Extended by:
ActiveSupport::Concern
Included in:
Xommelier::Xml::Element
Defined in:
lib/xommelier/xml/element/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SERIALIZATION_OPTIONS =
{
  encoding: 'utf-8'
}.freeze
SAVE_OPTIONS =
[:save_with, :indent_text, :indent].freeze

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/xommelier/xml/element/serialization.rb', line 148

def <=>(other)
  if text? && other.is_a?(String)
    text.to_s <=> other
  else
    super
  end
end

#==(other) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/xommelier/xml/element/serialization.rb', line 156

def ==(other)
  if text? && other.is_a?(String)
    text.to_s == other
  else
    super
  end
end

#=~(other) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/xommelier/xml/element/serialization.rb', line 164

def =~(other)
  if text? && other.is_a?(Regexp)
    text.to_s =~ other
  else
    super
  end
end

#from_xml(xml, options = {}) ⇒ Object Also known as: from_xommelier



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xommelier/xml/element/serialization.rb', line 54

def from_xml(xml, options = {})
  xml = Nokogiri::XML(xml) if IO === xml || String === xml
  @_xml_node = options.delete(:node) { xml.at_xpath(element_xpath(xml.document, element_name)) }

  self.text = @_xml_node.inner_html if text? && @_xml_node.inner_html.present?

  self.class.attributes.values.each do |attribute|
    deserialize_attribute(attribute)
  end

  self.class.elements.values.each do |element|
    deserialize_element(element)
  end
end

#to_hashObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/xommelier/xml/element/serialization.rb', line 123

def to_hash
  attributes.dup.tap do |hash|
    @elements.each do |name, value|
      element = element_options(name)
      if element.multiple?
        if value.count > 1
          name = element.plural
          value = value.map(&:to_hash) if element.complex_type?
        else
          value = value.first.to_hash
        end
      else
        value = value.to_hash if element.complex_type?
      end
      hash[name] = value
    end
    hash[:text] = text if text?
  end
end

#to_nokogiriNokogiri::XML::Node

Returns:

  • (Nokogiri::XML::Node)


144
145
146
# File 'lib/xommelier/xml/element/serialization.rb', line 144

def to_nokogiri
  ensure_xml_document.root
end

#to_sObject



172
173
174
175
176
177
178
# File 'lib/xommelier/xml/element/serialization.rb', line 172

def to_s
  if text?
    text.to_s
  else
    super
  end
end

#to_xml(options = {}) ⇒ Object Also known as: to_xommelier



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/xommelier/xml/element/serialization.rb', line 71

def to_xml(options = {})
  options = SERIALIZATION_OPTIONS.merge(options)
  save_options = options.slice(:encoding, *SAVE_OPTIONS)
  options = options.except(*SAVE_OPTIONS)

  element_name = options.delete(:element_name) { self.element_name }
  element_name = element_name.to_s
  element_name << '_' if %w(text class id).include?(element_name)
  xmlns = options[:ns] || self.xmlns
  if options[:builder] # Non-root element
    builder = options.delete(:builder)
    attribute_values = {}
    namespaces = builder.doc.namespaces
    prefix = options[:prefix] || namespaces.key(xmlns.uri).try(:[], 6..-1).presence
  else # Root element
    builder = Nokogiri::XML::Builder.new(options)
    attribute_values = children_namespaces.inject(xmlns: xmlns.uri) do |hash, ns|
      hash["xmlns:#{ns.as}"] = ns.uri
      hash
    end
    attribute_values.delete("xmlns:#{xmlns.as}")
    attribute_values.delete('xmlns:xml')
    attribute_values.delete(:xmlns) if self.xmlns.default?
    namespaces = attribute_values
    prefix = nil
  end
  current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns']
  attributes.each do |name, value|
    attribute = attribute_options(name)
    attribute_name = attribute.attribute_name
    ns = attribute.ns
    if ns.uri != current_xmlns && (attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence)
      attribute_name = "#{attr_prefix}:#{attribute_name}"
    end
    serialize_attribute(attribute_name, value, attribute_values) if !value.nil? || attribute.required?
  end
  @_xml_node = (prefix ? builder[prefix] : builder)
               .send(element_name, attribute_values) do |xml|
    self.class.elements.each do |name, element|
      value = elements.fetch(name, options[:default])
      next if value.nil?
      element.override(xmlns: xmlns) do
        serialize_element(name, value, xml, element)
      end
    end
    xml.text(@text) if text?
  end.instance_variable_get(:@node)
  builder.to_xml(save_options)
end