Class: Heisencoin::Plan
- Inherits:
-
Object
- Object
- Heisencoin::Plan
- Defined in:
- lib/heisencoin/plan.rb
Constant Summary collapse
- @@states =
["planning", "buying", "moving-in", "selling", "moving-out"]
Instance Attribute Summary collapse
-
#purse ⇒ Object
Returns the value of attribute purse.
-
#spent ⇒ Object
Returns the value of attribute spent.
-
#state ⇒ Object
Returns the value of attribute state.
-
#steps ⇒ Object
Returns the value of attribute steps.
Instance Method Summary collapse
- #<<(trades) ⇒ Object
- #cost ⇒ Object
- #from_simple(simple) ⇒ Object
-
#initialize(simple = nil) ⇒ Plan
constructor
A new instance of Plan.
- #profit ⇒ Object
- #quantity ⇒ Object
- #to_simple ⇒ Object
Constructor Details
#initialize(simple = nil) ⇒ Plan
Returns a new instance of Plan.
7 8 9 10 11 |
# File 'lib/heisencoin/plan.rb', line 7 def initialize(simple = nil) @steps = [] @state = @@states.first from_simple(simple) if simple end |
Instance Attribute Details
#purse ⇒ Object
Returns the value of attribute purse.
3 4 5 |
# File 'lib/heisencoin/plan.rb', line 3 def purse @purse end |
#spent ⇒ Object
Returns the value of attribute spent.
3 4 5 |
# File 'lib/heisencoin/plan.rb', line 3 def spent @spent end |
#state ⇒ Object
Returns the value of attribute state.
3 4 5 |
# File 'lib/heisencoin/plan.rb', line 3 def state @state end |
#steps ⇒ Object
Returns the value of attribute steps.
3 4 5 |
# File 'lib/heisencoin/plan.rb', line 3 def steps @steps end |
Instance Method Details
#<<(trades) ⇒ Object
27 28 29 30 |
# File 'lib/heisencoin/plan.rb', line 27 def <<(trades) @steps += trades self end |
#cost ⇒ Object
40 41 42 |
# File 'lib/heisencoin/plan.rb', line 40 def cost @steps.reduce(0){|total, trade| total+trade.cost} end |
#from_simple(simple) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/heisencoin/plan.rb', line 13 def from_simple(simple) @state = simple["state"] @purse = simple["purse"] @spent = simple["spent"] @steps = simple["steps"].map{|trade_simple| Trade.new(trade_simple)} end |
#profit ⇒ Object
36 37 38 |
# File 'lib/heisencoin/plan.rb', line 36 def profit @steps.reduce(0){|total, trade| total+trade.profit} end |
#quantity ⇒ Object
32 33 34 |
# File 'lib/heisencoin/plan.rb', line 32 def quantity @steps.reduce(0){|sum,trade|sum+trade.quantity} end |
#to_simple ⇒ Object
20 21 22 23 24 25 |
# File 'lib/heisencoin/plan.rb', line 20 def to_simple {"state" => @state, "purse" => @purse, "spent" => @spent, "steps" => @steps.map{|step| step.to_simple}} end |