From 3fae95e544e6b8d3e96117bf822e4a7372a729c1 Mon Sep 17 00:00:00 2001 From: Vlatko Ristovski Date: Sat, 17 Dec 2016 20:34:31 +0100 Subject: [PATCH] Better readme on static / dynamic definitons chapter --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b7375d..3b974c2 100644 --- a/README.md +++ b/README.md @@ -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| @@ -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 @@ -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