Class: Heisencoin::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/heisencoin/plan.rb

Constant Summary collapse

@@states =
["planning", "buying", "moving-in", "selling", "moving-out"]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#purseObject

Returns the value of attribute purse.



3
4
5
# File 'lib/heisencoin/plan.rb', line 3

def purse
  @purse
end

#spentObject

Returns the value of attribute spent.



3
4
5
# File 'lib/heisencoin/plan.rb', line 3

def spent
  @spent
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/heisencoin/plan.rb', line 3

def state
  @state
end

#stepsObject

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

#costObject



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

#profitObject



36
37
38
# File 'lib/heisencoin/plan.rb', line 36

def profit
  @steps.reduce(0){|total, trade| total+trade.profit}
end

#quantityObject



32
33
34
# File 'lib/heisencoin/plan.rb', line 32

def quantity
  @steps.reduce(0){|sum,trade|sum+trade.quantity}
end

#to_simpleObject



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