Module: OfxParser::MonetarySupport

Included in:
BankAccount, CreditAccount, Transaction
Defined in:
lib/ofx.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
# File 'lib/ofx.rb', line 31

def method_missing(meth, *args) #:nodoc:
  if (monetary_method_call?(meth))
    pennies_for(send(original_method(meth)))
  else
    super
  end
end

Instance Method Details

#monetary_method_call?(meth) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/ofx.rb', line 26

def monetary_method_call?(meth) #:nodoc:
  orig = original_method(meth)
  @@monies.include?(orig) && meth.to_s == "#{orig}_in_pennies"
end

#original_method(meth) ⇒ Object

:nodoc:



22
23
24
# File 'lib/ofx.rb', line 22

def original_method(meth) #:nodoc:
  meth.to_s.sub('_in_pennies','').to_sym rescue nil
end

#pennies_for(amount) ⇒ Object

Returns pennies for a given string amount, i.e:

'-123.45' => -12345
'123' => 12300


15
16
17
18
19
20
# File 'lib/ofx.rb', line 15

def pennies_for(amount)
  return nil if amount == ""
  int, fraction = amount.scan(/\d+/)
  i = (fraction.to_s.strip =~ /[1-9]/) ? "#{int}#{fraction[0,2]}".to_i : int.to_i * 100
  amount =~ /^\s*-\s*\d+/ ? -i : i
end

#respond_to?(meth) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


39
40
41
# File 'lib/ofx.rb', line 39

def respond_to?(meth) #:nodoc:
  monetary_method_call?(meth) ? true : super
end