Method: CalcProfit::Transaction#initialize
- Defined in:
- lib/calc_profit/transaction.rb
#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 |