Module: StateShifter::Definition::InstanceMethods
- Defined in:
- lib/state_shifter/definition/instance_methods.rb
Instance Method Summary collapse
- #call_state_entry_callback(trigger, old_state) ⇒ Object
- #check_event_callbacks(event_name) ⇒ Object
- #check_guards(event_name) ⇒ Object
- #current_state ⇒ Object
- #current_state_def ⇒ Object
- #event_names ⇒ Object
- #get_current_state ⇒ Object
- #halt(message) ⇒ Object
- #initial_state ⇒ Object
- #names_for(what) ⇒ Object
- #next_states ⇒ Object
- #set_current_state(value) ⇒ Object
- #state_machine_definition ⇒ Object
- #state_names ⇒ Object
- #transition(args) ⇒ Object
- #transitionable_states ⇒ Object
Instance Method Details
#call_state_entry_callback(trigger, old_state) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 55 def call_state_entry_callback trigger, old_state proc_or_method_name = current_state_def.entry_callback if proc_or_method_name.is_a?(Symbol) method_args = current_state_def.entry_callback_args if method_args self.send proc_or_method_name, method_args else self.send proc_or_method_name end else self.instance_exec(old_state, trigger.to_sym, &proc_or_method_name) end end |
#check_event_callbacks(event_name) ⇒ Object
45 46 47 48 49 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 45 def check_event_callbacks event_name event_def = state_machine_definition.get(:event, event_name) self.send event_def.callback end |
#check_guards(event_name) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 93 def check_guards event_name event = state_machine_definition.get(:event, event_name) if event.has_guards? event.guards.each do |guard| begin return false, guard unless self.send(guard.to_sym) rescue NoMethodError raise GuardMethodUndefined, guard end end true else true end end |
#current_state ⇒ Object
5 6 7 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 5 def current_state get_current_state end |
#current_state_def ⇒ Object
51 52 53 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 51 def current_state_def state_machine_definition.get(:state, get_current_state) end |
#event_names ⇒ Object
33 34 35 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 33 def event_names names_for :events end |
#get_current_state ⇒ Object
9 10 11 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 9 def get_current_state instance_variable_defined?(:@current_state) ? @current_state.to_sym : state_machine_definition.initial_state.name.to_sym end |
#halt(message) ⇒ Object
89 90 91 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 89 def halt raise TransitionHalted, end |
#initial_state ⇒ Object
37 38 39 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 37 def initial_state state_machine_definition.initial_state.name end |
#names_for(what) ⇒ Object
41 42 43 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 41 def names_for what state_machine_definition.send(what).collect {|name, definition| name.to_sym } end |
#next_states ⇒ Object
21 22 23 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 21 def next_states _next_states get_current_state end |
#set_current_state(value) ⇒ Object
13 14 15 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 13 def set_current_state value @current_state = value.to_sym end |
#state_machine_definition ⇒ Object
17 18 19 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 17 def state_machine_definition self.class.state_machine_definition end |
#state_names ⇒ Object
29 30 31 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 29 def state_names names_for :states end |
#transition(args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 72 def transition args _start = Time.now # BOOP! old_state = get_current_state set_current_state args[:to] # self.instance_exec(old_state, get_current_state, args[:trigger].to_sym, (Time.now - _start), &state_machine_definition.on_transition_proc) if state_machine_definition.has_on_transition_proc? call_state_entry_callback(args[:trigger], old_state) if current_state_def.has_entry_callback? check_event_callbacks(args[:trigger]) if state_machine_definition.get(:event, args[:trigger]).has_callback? true end |
#transitionable_states ⇒ Object
25 26 27 |
# File 'lib/state_shifter/definition/instance_methods.rb', line 25 def transitionable_states _next_states get_current_state, {:check_guards => true} end |