Class: Financial::Account
- Inherits:
-
Object
- Object
- Financial::Account
- Defined in:
- lib/financial/account.rb
Instance Attribute Summary collapse
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other_account) ⇒ Object
- #account_manager ⇒ Object
- #all_costs ⇒ Object
- #all_revenues ⇒ Object
- #calculate(method_name) ⇒ Object
- #calculate_balances ⇒ Object (also: #balances)
- #costs(&block) ⇒ Object
- #costs_in_date(date) ⇒ Object
- #deposits(&block) ⇒ Object
- #events ⇒ Object
- #events_in_date(date) ⇒ Object
- #find_balance_in_date(date) ⇒ Object
-
#initialize(*arguments) ⇒ Account
constructor
A new instance of Account.
-
#last_balance_in_date(date) ⇒ Object
OPTIMIZE ME: if this have hundreds balances will slow things down :.
-
#received_deposits ⇒ Object
FIXME: Fix a bug that dups all received_deposits.
- #revenues(&block) ⇒ Object
- #revenues_in_date(date) ⇒ Object
- #taxes ⇒ Object
- #total(value = nil) ⇒ Object
- #total_costs ⇒ Object
- #total_revenues ⇒ Object
- #unique_events_dates ⇒ Object
Constructor Details
#initialize(*arguments) ⇒ Account
Returns a new instance of Account.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/financial/account.rb', line 5 def initialize(*arguments) arguments.flatten! = arguments.last.is_a?(Hash) ? arguments.pop : {} @name = arguments.shift raise RuntimeError, "Pass a name to account" unless @name @banner = [:as] || @name.to_s.split('_').join(' ').capitalize @costs = Costs.new @revenues = Revenues.new @deposits = Deposits.new(@name) @balances = [] @total = 0 end |
Instance Attribute Details
#banner ⇒ Object
Returns the value of attribute banner.
3 4 5 |
# File 'lib/financial/account.rb', line 3 def @banner end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/financial/account.rb', line 3 def name @name end |
Class Method Details
.find(options) ⇒ Object
121 122 123 |
# File 'lib/financial/account.rb', line 121 def self.find() Financial.account_manager.accounts.find { |account| account.name == [:name].to_sym} end |
Instance Method Details
#==(other_account) ⇒ Object
102 103 104 |
# File 'lib/financial/account.rb', line 102 def ==(other_account) name == other_account.name end |
#account_manager ⇒ Object
125 126 127 |
# File 'lib/financial/account.rb', line 125 def account_manager Financial.account_manager end |
#all_costs ⇒ Object
48 49 50 |
# File 'lib/financial/account.rb', line 48 def all_costs [costs, deposits, taxes].flatten.sort_by { |event| event.date } end |
#all_revenues ⇒ Object
52 53 54 |
# File 'lib/financial/account.rb', line 52 def all_revenues [revenues, received_deposits].flatten end |
#calculate(method_name) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/financial/account.rb', line 84 def calculate(method_name) objects_instances = self.send(method_name) costs_or_revenues_sum = if objects_instances.empty? 0 else objects_instances.collect { |object_instance| object_instance.value }.inject(:+) end self.instance_variable_set("@#{method_name.to_s.gsub('all', 'total')}", costs_or_revenues_sum) end |
#calculate_balances ⇒ Object Also known as: balances
43 44 45 |
# File 'lib/financial/account.rb', line 43 def calculate_balances @balances = BalanceCalculation.new(self).calculate! end |
#costs(&block) ⇒ Object
28 29 30 31 32 |
# File 'lib/financial/account.rb', line 28 def costs(&block) @costs.instance_eval(&block) if block_given? @costs.replace_parcels_with_costs! @costs end |
#costs_in_date(date) ⇒ Object
68 69 70 |
# File 'lib/financial/account.rb', line 68 def costs_in_date(date) all_costs.select { |cost| cost.date == date } end |
#deposits(&block) ⇒ Object
23 24 25 26 |
# File 'lib/financial/account.rb', line 23 def deposits(&block) @deposits.instance_eval(&block) if block_given? @deposits end |
#events ⇒ Object
60 61 62 |
# File 'lib/financial/account.rb', line 60 def events [all_costs, all_revenues].flatten end |
#events_in_date(date) ⇒ Object
64 65 66 |
# File 'lib/financial/account.rb', line 64 def events_in_date(date) events.select {|event| event.date == date } end |
#find_balance_in_date(date) ⇒ Object
117 118 119 |
# File 'lib/financial/account.rb', line 117 def find_balance_in_date(date) balances.find { |balance| balance.date == date } end |
#last_balance_in_date(date) ⇒ Object
OPTIMIZE ME: if this have hundreds balances will slow things down :\
108 109 110 111 112 113 114 115 |
# File 'lib/financial/account.rb', line 108 def last_balance_in_date(date) all_balances_for_date = balances.select { |balance| balance.date <= date } if all_balances_for_date.empty? total else all_balances_for_date.last.value end end |
#received_deposits ⇒ Object
FIXME: Fix a bug that dups all received_deposits
96 97 98 99 100 |
# File 'lib/financial/account.rb', line 96 def received_deposits account_manager.accounts.collect { |account| account.deposits}.flatten.select do |deposit| deposit.account_to_deposit.equal?(@name) end end |
#revenues(&block) ⇒ Object
34 35 36 37 |
# File 'lib/financial/account.rb', line 34 def revenues(&block) @revenues.instance_eval(&block) if block_given? @revenues end |
#revenues_in_date(date) ⇒ Object
72 73 74 |
# File 'lib/financial/account.rb', line 72 def revenues_in_date(date) all_revenues.select { |revenue| revenue.date == date } end |
#taxes ⇒ Object
39 40 41 |
# File 'lib/financial/account.rb', line 39 def taxes revenues.collect { |revenue| revenue.tax }.compact end |
#total(value = nil) ⇒ Object
18 19 20 21 |
# File 'lib/financial/account.rb', line 18 def total(value=nil) return @total unless value @total = value end |
#total_costs ⇒ Object
76 77 78 |
# File 'lib/financial/account.rb', line 76 def total_costs calculate(:all_costs) end |
#total_revenues ⇒ Object
80 81 82 |
# File 'lib/financial/account.rb', line 80 def total_revenues calculate(:all_revenues) end |
#unique_events_dates ⇒ Object
56 57 58 |
# File 'lib/financial/account.rb', line 56 def unique_events_dates events.collect { |event| event.date }.uniq.sort end |