Class: ONIX::DateType

Inherits:
ROXML::XMLRef
  • Object
show all
Defined in:
lib/onix/date_type.rb

Overview

Internal class representing XML content date binding

In context:

<element attribute="XMLAttributeRef">
 XMLYYYYMMDDRef
</element>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessor, args, &block) ⇒ DateType

Returns a new instance of DateType.



14
15
16
17
18
# File 'lib/onix/date_type.rb', line 14

def initialize(accessor, args, &block)
  super(accessor, args, &block)
  @content = args.content?
  @cdata = args.cdata?
end

Instance Attribute Details

#cdataObject (readonly)

::nodoc


12
13
14
# File 'lib/onix/date_type.rb', line 12

def cdata
  @cdata
end

#contentObject (readonly)

::nodoc


12
13
14
# File 'lib/onix/date_type.rb', line 12

def content
  @content
end

Instance Method Details

#update_xml(xml, value) ⇒ Object

Updates the text in the given xml block to the value provided.



22
23
24
25
26
# File 'lib/onix/date_type.rb', line 22

def update_xml(xml, value)
  parent = wrap(xml)
  add(parent.child_add(LibXML::XML::Node.new_element(name)), value.strftime("%Y%m%d"))
  xml
end

#value(xml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/onix/date_type.rb', line 28

def value(xml)
  begin
    if content
      value = Date.parse(xml.content.strip)
    else
      child = xml.search(name).first
      value = Date.parse(child.content.strip) if child
    end
  rescue ArgumentError
    value = nil
  end
  block ? block.call(value) : value
end