Class: Invoicing::LedgerItem::AccountSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/invoicing/ledger_item.rb

Overview

Very simple class for representing the sum of all sales, purchases and payments on an account.

Constant Summary collapse

NUM_FIELDS =

:nodoc:

[:sales, :purchases, :sale_receipts, :purchase_payments, :balance]

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ AccountSummary

Returns a new instance of AccountSummary.



819
820
821
822
823
# File 'lib/invoicing/ledger_item.rb', line 819

def initialize(hash)
  @currency = hash[:currency]; @sales = hash[:sales]; @purchases = hash[:purchases]
  @sale_receipts = hash[:sale_receipts]; @purchase_payments = hash[:purchase_payments]
  @balance = hash[:balance]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



825
826
827
828
829
830
831
# File 'lib/invoicing/ledger_item.rb', line 825

def method_missing(name, *args)
  if name.to_s =~ /(.*)_formatted$/
    ::Invoicing::CurrencyValue::Formatter.format_value(currency, send($1))
  else
    super
  end
end

Instance Method Details

#+(other) ⇒ Object



833
834
835
836
837
# File 'lib/invoicing/ledger_item.rb', line 833

def +(other)
  hash = {:currency => currency}
  NUM_FIELDS.each {|field| hash[field] = send(field) + other.send(field) }
  AccountSummary.new hash
end

#to_sObject



839
840
841
842
843
844
# File 'lib/invoicing/ledger_item.rb', line 839

def to_s
  NUM_FIELDS.map do |field|
    val = send("#{field}_formatted")
    "#{field} = #{val}"
  end.join('; ')
end