Class: CalcProfit::Transaction
- Inherits:
-
Object
- Object
- CalcProfit::Transaction
- Defined in:
- lib/calc_profit/transaction.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#raw_shares ⇒ Object
readonly
Returns the value of attribute raw_shares.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
-
#shares ⇒ Object
Returns the value of attribute shares.
Instance Method Summary collapse
-
#initialize(trans_hash) ⇒ Transaction
constructor
A new instance of Transaction.
- #purchase? ⇒ Boolean
- #sale? ⇒ Boolean
- #table_row ⇒ Object
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/calc_profit/transaction.rb', line 5 def code @code end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
5 6 7 |
# File 'lib/calc_profit/transaction.rb', line 5 def date @date end |
#info ⇒ Object (readonly)
Returns the value of attribute info.
5 6 7 |
# File 'lib/calc_profit/transaction.rb', line 5 def info @info end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
5 6 7 |
# File 'lib/calc_profit/transaction.rb', line 5 def price @price end |
#raw_shares ⇒ Object (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 |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
5 6 7 |
# File 'lib/calc_profit/transaction.rb', line 5 def ref @ref end |
#shares ⇒ Object
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
42 43 44 |
# File 'lib/calc_profit/transaction.rb', line 42 def purchase? code == 'P' end |
#sale? ⇒ Boolean
46 47 48 |
# File 'lib/calc_profit/transaction.rb', line 46 def sale? code == 'S' end |
#table_row ⇒ Object
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 |