Class: MoneyNode

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) extract_attr_value(xml)



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

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

- (Object) initialize_impl(path)



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

def initialize_impl(path)
  @amount_path = XML::XXPath.new(path + "/")
  @currency_path = XML::XXPath.new(path + "/@currencyID")
end

- (Object) set_attr_value(xml, value)



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

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