Class: Rebalance::Account

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Account

Returns a new instance of Account.



5
6
7
8
9
10
11
# File 'lib/rebalance/account.rb', line 5

def initialize(name, &block)
  self.name = name
  self.funds = {}
  self.rebalanced_funds = {}

  instance_eval &block
end

Instance Attribute Details

#fundsObject

Returns the value of attribute funds.



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

def funds
  @funds
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#rebalanced_fundsObject

Returns the value of attribute rebalanced_funds.



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

def rebalanced_funds
  @rebalanced_funds
end

Instance Method Details

#calculate_percentagesObject



26
27
28
29
30
31
32
# File 'lib/rebalance/account.rb', line 26

def calculate_percentages
  percentages = {}
  funds.each do |symbol, fund|
    percentages[fund.symbol] = (fund.value / total_value * 100).round(2)
  end
  percentages
end

#find_by_asset_class(asset_class) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rebalance/account.rb', line 34

def find_by_asset_class(asset_class)
  asset_class_funds = []
  funds.each do |symbol, fund|
    asset_class_funds << fund if fund.asset_class == asset_class
  end
  asset_class_funds
end

#fund(symbol, asset_class, shares, price = nil) ⇒ Object



13
14
15
16
# File 'lib/rebalance/account.rb', line 13

def fund(symbol, asset_class, shares, price=nil)
  new_fund = Fund.new(symbol, asset_class, shares, price)
  self.funds[new_fund.symbol] = new_fund
end

#total_valueObject



18
19
20
21
22
23
24
# File 'lib/rebalance/account.rb', line 18

def total_value
  total_value = 0
  funds.each do |symbol, fund|
    total_value = total_value + fund.value
  end
  total_value
end