Module: OfxParser::MonetarySupport

Included in:
BankAccount, CreditAccount, InvestmentAccount, 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:



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

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)


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

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

#original_method(meth) ⇒ Object

:nodoc:



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

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


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

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)


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

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