Class: XML::MappingExtensions::DateNode

Inherits:
NodeBase
  • Object
show all
Defined in:
lib/xml/mapping_extensions/date_node.rb

Overview

XML mapping for XML Schema dates. Known limitation: loses time zone info (Ruby Date doesn't support it)

Instance Method Summary collapse

Methods inherited from NodeBase

#extract_attr_value, #initialize, #set_attr_value

Constructor Details

This class inherits a constructor from XML::MappingExtensions::NodeBase

Instance Method Details

#to_value(xml_text) ⇒ Date

Converts an XML Schema date string to a Date object

Parameters:

  • xml_text (String)

    an XML Schema date

Returns:

  • (Date)

    the value as a Date



22
23
24
# File 'lib/xml/mapping_extensions/date_node.rb', line 22

def to_value(xml_text)
  Date.xmlschema(xml_text)
end

#to_xml_text(value) ⇒ String

Converts a Date object to an XML schema string

Parameters:

  • value (Date)

    the value as a Date

Returns:

  • (String)

    the value as an XML Schema date string, without time zone information unless #zulu is set



30
31
32
33
34
# File 'lib/xml/mapping_extensions/date_node.rb', line 30

def to_xml_text(value)
  value = value.to_date if value.respond_to?(:to_date)
  text = value.iso8601 # use iso8601 instead of xmlschema in case of ActiveSupport shenanigans
  zulu && !text.end_with?('Z') ? "#{text}Z" : text
end

#zuluBoolean?

Whether date should be output with UTC "Zulu" time designator ("Z")

Returns:

  • (Boolean, nil)

    True if date should be output with "Z" time designator, false or nil otherwise



15
16
17
# File 'lib/xml/mapping_extensions/date_node.rb', line 15

def zulu
  @options[:zulu]
end