Class: XmlParsable::Elements::NumericElement

Inherits:
AbstractElement show all
Defined in:
lib/xmlparsable/elements/numeric.rb

Overview

Parses content into a ruby Numeric value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractElement

#comment, #open, parsable

Constructor Details

#initialize(name, attributes, parent, arguments) ⇒ NumericElement

Returns a new instance of NumericElement.



9
10
11
12
# File 'lib/xmlparsable/elements/numeric.rb', line 9

def initialize(name, attributes, parent, arguments)
  @name, @attributes, @parent, @string =
    name, attributes, parent, ""
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/xmlparsable/elements/numeric.rb', line 7

def name
  @name
end

Instance Method Details

#finalizeObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xmlparsable/elements/numeric.rb', line 18

def finalize
  stripped = @string.strip
  numeric  = if stripped =~ /[e.]/i and stripped !~ /x/i
               BigDecimal(stripped)
             elsif stripped != ""
               Integer(stripped)
             end

  # TODO: Subclass BigDecimal and Integer to add an accessor method
  numeric.instance_variable_set(:@xmlattrs, @attributes)
  @parent.close(self, numeric)
end

#read(text) ⇒ Object



14
15
16
# File 'lib/xmlparsable/elements/numeric.rb', line 14

def read(text)
  @string << text
end