Sha256: 7430a5c59e013ea8b90ac3021609160d70a1f62a070acc4f9bb4543f68e32f6a

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

require_relative 'node_base'

require 'time'

module XML
  module MappingExtensions
    # XML mapping for XML Schema dates.
    # Known limitation: loses time zone info (Ruby `Date` doesn't
    # support it)
    class DateNode < NodeBase
      # Whether date should be output with UTC "Zulu" time
      # designator ("Z")
      # @return [Boolean, nil] True if date should be output
      #   with "Z" time designator, false or nil otherwise
      def zulu
        @options[:zulu]
      end

      # Converts an XML Schema date string to a `Date` object
      # @param xml_text [String] an XML Schema date
      # @return [Date] the value as a `Date`
      def to_value(xml_text)
        Date.xmlschema(xml_text)
      end

      # Converts a `Date` object to an XML schema string
      # @param value [Date] the value as a `Date`
      # @return [String] the value as an XML Schema date string, without
      #   time zone information unless {#zulu} is set
      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
    end
    ::XML::Mapping.add_node_class DateNode
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
xml-mapping_extensions-0.4.7 lib/xml/mapping_extensions/date_node.rb
xml-mapping_extensions-0.4.6 lib/xml/mapping_extensions/date_node.rb
xml-mapping_extensions-0.4.5 lib/xml/mapping_extensions/date_node.rb
xml-mapping_extensions-0.4.4 lib/xml/mapping_extensions/date_node.rb
xml-mapping_extensions-0.4.3 lib/xml/mapping_extensions/date_node.rb
xml-mapping_extensions-0.4.2 lib/xml/mapping_extensions/date_node.rb