module MicroStateMachine
Public Class Methods
included(klass)
click to toggle source
# File lib/micro_state_machine.rb, line 11 def self.included(klass) klass.class_attribute :_on_enter_state, :_on_exit_state, :_initial_state, :_after_state_change, :_states klass.send(:extend, ClassMethods) if klass.respond_to?(:after_initialize) klass.instance_eval do after_initialize(:set_initial_state) end end end
Public Instance Methods
can_transition_to?(new_state)
click to toggle source
# File lib/micro_state_machine.rb, line 106 def can_transition_to?(new_state) states = self.class._states options = HashWithIndifferentAccess.new(states)[new_state] options.blank? || options[:from].include?(get_state) end
get_state()
click to toggle source
# File lib/micro_state_machine.rb, line 91 def get_state send(state_column) end
is?(state)
click to toggle source
# File lib/micro_state_machine.rb, line 102 def is?(state) get_state.to_s == state.to_s end
set_initial_state()
click to toggle source
# File lib/micro_state_machine.rb, line 95 def set_initial_state current_value = send(state_column) if current_value.nil? send("#{state_column}=", self.class._initial_state) end end
state_column()
click to toggle source
Instance Methods
# File lib/micro_state_machine.rb, line 87 def state_column self.class.state_column end
transition_to(new_state)
click to toggle source
# File lib/micro_state_machine.rb, line 112 def transition_to(new_state) old_state = get_state transition_method_name = "transition_from_#{old_state}_to_#{new_state}" raise InvalidState.new(old_state, new_state) unless respond_to?(transition_method_name) send(transition_method_name) self.class._after_state_change.each do |blk| instance_exec(old_state, new_state, &blk) end self end