Class: Seiun::XMLParsers::Base
- Inherits:
-
Object
- Object
- Seiun::XMLParsers::Base
- Defined in:
- lib/seiun/xml_parsers/base.rb
Instance Method Summary collapse
-
#initialize(attrs) ⇒ Base
constructor
A new instance of Base.
- #to_hash(strict_mode = false) ⇒ Object
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 |