Class: SimplerStateMachine::Base
- Inherits:
-
Object
- Object
- SimplerStateMachine::Base
- Defined in:
- lib/simpler_state_machine/base.rb
Instance Attribute Summary collapse
-
#clazz ⇒ Object
Returns the value of attribute clazz.
-
#events ⇒ Object
readonly
Holds the state enum.
-
#initial_state(initial_state = nil) ⇒ Object
readonly
Holds the state enum.
-
#persistency_method(method_name = nil) ⇒ Object
readonly
Holds the state enum.
-
#state_field(state_field = nil) ⇒ Object
readonly
Holds the state enum.
-
#states ⇒ Object
readonly
Holds the state enum.
-
#states_constant_name(constant_name = nil) ⇒ Object
readonly
Holds the state enum.
-
#transitions ⇒ Object
readonly
Holds the state enum.
Instance Method Summary collapse
-
#add_state(state_name, options = {}) ⇒ Object
Add a new state.
-
#add_transition(transition_name, options = {}) ⇒ Object
Add transition.
-
#enumize! ⇒ Object
Transform the states array to enum.
-
#initialize(clazz) ⇒ Base
constructor
A new instance of Base.
-
#initialize_copy(orig) ⇒ Object
A #clone callback.
-
#persistent_mode? ⇒ Boolean
Persistency method.
Constructor Details
#initialize(clazz) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 22 |
# File 'lib/simpler_state_machine/base.rb', line 14 def initialize(clazz) @clazz = clazz @_states = [] @events = {} @transitions = {} @state_field = "status" @states_constant_name = "STATES" @persistency_method = nil end |
Instance Attribute Details
#clazz ⇒ Object
Returns the value of attribute clazz.
12 13 14 |
# File 'lib/simpler_state_machine/base.rb', line 12 def clazz @clazz end |
#events ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def events @events end |
#initial_state(initial_state = nil) ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def initial_state @initial_state end |
#persistency_method(method_name = nil) ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def persistency_method @persistency_method end |
#state_field(state_field = nil) ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def state_field @state_field end |
#states ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def states @states end |
#states_constant_name(constant_name = nil) ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def states_constant_name @states_constant_name end |
#transitions ⇒ Object (readonly)
Holds the state enum
4 5 6 |
# File 'lib/simpler_state_machine/base.rb', line 4 def transitions @transitions end |
Instance Method Details
#add_state(state_name, options = {}) ⇒ Object
Add a new state
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/simpler_state_machine/base.rb', line 71 def add_state(state_name, = {}) # Fill in missing with callbacks with empty procs. = { :before_enter => Proc.new { true }, :after_enter => Proc.new { true }, :before_leave => Proc.new { true }, :after_leave => Proc.new { true }, :on_error => Proc.new { |error| true } }.merge() # Append to the states arry @_states << human_state_name(state_name) # Register events @events[human_state_name(state_name)] = .dup @clazz.instance_eval do # define a "#state?" method on the object instance define_method("#{state_name}?") do self.send(self.class.state_machine.state_field) == self.class.state_machine.states[state_name].to_i end end end |
#add_transition(transition_name, options = {}) ⇒ Object
Add transition
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/simpler_state_machine/base.rb', line 96 def add_transition(transition_name, = {}) # Convert from states to array [:from] = [:from].is_a?(Array) ? [:from] : [[:from]] # Register transactions @transitions[transition_name.to_s] = @clazz.instance_eval do # Define the transition method define_method("#{transition_name}") do |*args| persist = args.first if args.any? persist |= false # check if the transition is available to the current state if self.class.state_machine.transitions[transition_name.to_s] && self.class.state_machine.transitions[transition_name.to_s][:from].include?(self.enum_status.to_sym.to_s) # Get the current state events current_state_events = self.class.state_machine.events[self.enum_status.to_sym.to_s] next_state_events = self.class.state_machine.events[self.class.state_machine.transitions[transition_name.to_s][:to]] begin # Fire events and perform transition (and persistence if needed) fire_state_machine_event(current_state_events[:before_leave]) fire_state_machine_event(next_state_events[:before_enter]) self.send("#{self.class.state_machine.state_field}=", self.class.state_machine.states[self.class.state_machine.transitions[transition_name.to_s][:to]].to_i) self.send(self.class.state_machine.persistency_method) if self.class.state_machine.persistent_mode? && persist == true fire_state_machine_event(current_state_events[:after_leave]) fire_state_machine_event(next_state_events[:after_enter]) rescue Exception => e fire_state_machine_event(current_state_events[:on_error]) end return true else return false end end # Define a transition method with a shebang define_method("#{transition_name}!") do if !(self.send(transition_name, true)) raise Exceptions::InvalidTransition, "Could not transit '#{self.enum_status.to_sym.to_s}' to '#{[:to]}'" else return true end end end end |
#enumize! ⇒ Object
Transform the states array to enum
148 149 150 |
# File 'lib/simpler_state_machine/base.rb', line 148 def enumize! @states = SimplerStateMachine::Enum.new(*@_states) end |
#initialize_copy(orig) ⇒ Object
A #clone callback. we ensure that even the sub elements are cloned - ‘deep copy’
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/simpler_state_machine/base.rb', line 25 def initialize_copy(orig) super @states = orig.states.clone @_states = orig.instance_variable_get("@_states").clone @events = orig.events.clone @transitions = orig.transitions.clone @initial_state = orig.initial_state.clone @state_field = orig.state_field @persistency_method = orig.persistency_method.clone unless @persistency_method.nil? @states_constant_name = orig.states_constant_name.clone end |
#persistent_mode? ⇒ Boolean
Persistency method
153 154 155 |
# File 'lib/simpler_state_machine/base.rb', line 153 def persistent_mode? !(@persistency_method.nil?) end |