Class: MoneyNode

Inherits:
XML::Mapping::SingleAttributeNode
  • Object
show all
Defined in:
lib/support/xml_mapping/money_node.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MoneyNode



5
6
7
8
9
# File 'lib/support/xml_mapping/money_node.rb', line 5

def initialize(*args)
  @path,*args = super(*args)
  @amount_path = XML::XXPath.new(@path)
  @currency_path = XML::XXPath.new(@path + "/@currencyID")
end

Instance Method Details

#extract_attr_value(xml) ⇒ Object



11
12
13
14
15
16
# File 'lib/support/xml_mapping/money_node.rb', line 11

def extract_attr_value(xml)
  amount, currency = default_when_xpath_err{ [(@amount_path.first(xml).text.to_f * 100).to_i,
                                   @currency_path.first(xml).text]
                                }
  Money.new(amount, currency)
end

#set_attr_value(xml, value) ⇒ Object



18
19
20
21
22
# File 'lib/support/xml_mapping/money_node.rb', line 18

def set_attr_value(xml, value)
  raise "Not a Money object: #{value}" unless Money === value
  @amount_path.first(xml, :ensure_created => true).text = sprintf("%.2f", value.cents.to_f / 100  )
  @currency_path.first(xml, :ensure_created => true).text = value.currency
end