Skip to content

Commit

Permalink
Better readme on static / dynamic definitons chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ristovskiv committed Dec 17, 2016
1 parent c9cef47 commit 3fae95e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Vehicle

state_machine :state, initial: :parked do
before_transition parked: any - :parked, do: :put_on_seatbelt

after_transition on: :crash, do: :tow
after_transition on: :repair, do: :fix
after_transition any => :parked do |vehicle, transition|
Expand Down Expand Up @@ -545,6 +545,10 @@ class Machine
def self.new(object, *args, &block)
machine_class = Class.new
machine = machine_class.state_machine(*args, &block)

# this is the `attribute` which stores the value for state in the class
# exaple for the Vehicle it would be `state` reader

attribute = machine.attribute
action = machine.action

Expand All @@ -556,7 +560,10 @@ class Machine
define_method(action) { object.send(action) } if action
end

machine_class.new
# This won't change the value of the `attribute` on the Vehicle class if it already
# has a value assigned to it. Otherwise it will set it to the initial one
state = object.send(attribute)
machine_class.new.tap{|machine| machine.send("#{attribute}=", state) if state}
end
end

Expand Down

0 comments on commit 3fae95e

Please sign in to comment.