Class: Seiun::XMLParsers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/seiun/xml_parsers/base.rb

Direct Known Subclasses

BatchXML, JobXML, RecordXML, ResultXML

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Base

Returns a new instance of Base.



14
15
16
# File 'lib/seiun/xml_parsers/base.rb', line 14

def initialize(attrs)
  @attrs = attrs
end

Instance Method Details

#to_hash(strict_mode = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/seiun/xml_parsers/base.rb', line 18

def to_hash(strict_mode = false)
  return {} unless @attrs.is_a?(Array)
  @attrs.each_with_object({}) do |attribute, result|
    key, values = attribute.to_a.first
    results = [values].flatten.map{|value|
      next if value.is_a?(Hash) && value["xsi:nil"] == "true"
      next value if strict_mode
      if Seiun::Utils.parsable_date?(value) && ( date = Date.parse(value, false) rescue nil )
        next date
      end
      if Seiun::Utils.parsable_time?(value) && ( time = Time.iso8601(value) rescue nil )
        next time
      end
      next true if value == "true"
      next false if value == "false"
      value
    }
    results.uniq! unless strict_mode
    result[key] = ( results.size <= 1 ? results.first : results )
  end
end