Class: CalcProfit::Transaction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trans_hash) ⇒ Transaction

Returns a new instance of Transaction.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/calc_profit/transaction.rb', line 8

def initialize(trans_hash)
  if trans_hash[:code] =~ /^\s*[pP]/
    @code = 'P'
  elsif trans_hash[:code] =~ /^\s*[sS]/
    @code = 'S'
  else
    raise "Bad code (#{code}) supplied to Transaction constructor."
  end
  @price = trans_hash[:price].to_f
  @shares = trans_hash[:shares].to_f.round
  @raw_shares = trans_hash[:raw_shares].to_f.round
  case trans_hash[:date]
  when String
    @date = Date.parse(trans_hash[:date])
  when Date
    @date = trans_hash[:date]
  else
    raise "Bad date (#{date}) supplied to Transaction constructor."
  end
  @ref = trans_hash[:ref]
  @info = trans_hash[:info]
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#priceObject (readonly)

Returns the value of attribute price.



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

def price
  @price
end

#raw_sharesObject (readonly)

Returns the value of attribute raw_shares.



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

def raw_shares
  @raw_shares
end

#refObject (readonly)

Returns the value of attribute ref.



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

def ref
  @ref
end

#sharesObject

Returns the value of attribute shares.



6
7
8
# File 'lib/calc_profit/transaction.rb', line 6

def shares
  @shares
end

Instance Method Details

#purchase?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/calc_profit/transaction.rb', line 42

def purchase?
  code == 'P'
end

#sale?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/calc_profit/transaction.rb', line 46

def sale?
  code == 'S'
end

#table_rowObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/calc_profit/transaction.rb', line 31

def table_row
  hash = {}
  hash[:ref] = ref
  hash[:date] = date.iso
  hash[:code] = code
  hash[:shares] = shares.to_s
  hash[:price] = price.to_s
  hash[:info] = info
  hash
end