Class: CalcProfit::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/calc_profit/match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, s) ⇒ Match

Returns a new instance of Match.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/calc_profit/match.rb', line 7

def initialize(p, s)
  case p
  when Transaction
    if p.code == 'P'
      @purchase = p
    else
      raise "First argument to Match (#{p}) not a purchase Transaction."
    end
  else
    raise "First argument to Match (#{p}) not a purchase Transaction."
  end

  case s
  when Transaction
    if s.code == 'S'
      @sale = s
    else
      raise "Second argument to Match (#{s}) not a sale Transaction."
    end
  else
    raise "Second argument to Match (#{s}) not a sale Transaction."
  end

  if p.price >= s.price
    raise "No profit from purchase @ #{p.price} and sale @ #{s.price} supplied to Match."
  end

  unless p.date.within_6mos_of?(s.date)
    raise "Match purchase and sale not within 6 months of each other."
  end
end

Instance Attribute Details

#purchaseObject (readonly)

Returns the value of attribute purchase.



5
6
7
# File 'lib/calc_profit/match.rb', line 5

def purchase
  @purchase
end

#saleObject (readonly)

Returns the value of attribute sale.



5
6
7
# File 'lib/calc_profit/match.rb', line 5

def sale
  @sale
end

Instance Method Details

#profitObject



39
40
41
42
43
# File 'lib/calc_profit/match.rb', line 39

def profit
  p = ((@sale.price - @purchase.price) *
       [@sale.shares, @purchase.shares].min)
  (p * 100.0).round / 100.0
end

#table_rowObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/calc_profit/match.rb', line 45

def table_row
  row = {}
  purchase.table_row.each_pair do |k, v|
    row[(k.entitle + ' P').as_sym] = v
  end
  sale.table_row.each_pair do |k, v|
    row[(k.entitle + ' S').as_sym] = v
  end
  row[:profit] = profit.to_s
  row
end