Class: Financial::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/financial/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Account

Returns a new instance of Account.

Raises:

  • (RuntimeError)


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/financial/account.rb', line 5

def initialize(*arguments)
  arguments.flatten!
  options = arguments.last.is_a?(Hash) ? arguments.pop : {}
  @name = arguments.shift
  raise RuntimeError, "Pass a name to account" unless @name
  @banner = options[:as] || @name.to_s.split('_').join(' ').capitalize
  @costs = Costs.new
  @revenues = Revenues.new
  @deposits = Deposits.new(@name)
  @balances = []
  @total = 0
end

Instance Attribute Details

Returns the value of attribute banner.



3
4
5
# File 'lib/financial/account.rb', line 3

def banner
  @banner
end

#nameObject

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(options)
  Financial..accounts.find { || .name == options[:name].to_sym}
end

Instance Method Details

#==(other_account) ⇒ Object



102
103
104
# File 'lib/financial/account.rb', line 102

def ==()
  name == .name
end

#account_managerObject



125
126
127
# File 'lib/financial/account.rb', line 125

def 
  Financial.
end

#all_costsObject



48
49
50
# File 'lib/financial/account.rb', line 48

def all_costs
  [costs, deposits, taxes].flatten.sort_by { |event| event.date }
end

#all_revenuesObject



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_balancesObject 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

#eventsObject



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_depositsObject

FIXME: Fix a bug that dups all received_deposits



96
97
98
99
100
# File 'lib/financial/account.rb', line 96

def received_deposits
  .accounts.collect { || .deposits}.flatten.select do |deposit|
    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

#taxesObject



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_costsObject



76
77
78
# File 'lib/financial/account.rb', line 76

def total_costs
  calculate(:all_costs)
end

#total_revenuesObject



80
81
82
# File 'lib/financial/account.rb', line 80

def total_revenues
  calculate(:all_revenues)
end

#unique_events_datesObject



56
57
58
# File 'lib/financial/account.rb', line 56

def unique_events_dates
  events.collect { |event| event.date }.uniq.sort
end