Class: StateMachine::Transition Abstract
- Inherits:
-
Object
- Object
- StateMachine::Transition
- Defined in:
- lib/motion-state-machine/transition.rb
Overview
Subclass and override #event_description, #arm and #unarm to implement a custom Transition class.
Direct Known Subclasses
NotificationTransition, SendEventTransition, TimedTransition
Constant Summary collapse
- @@types_to_subclasses =
{}
Class Attribute Summary collapse
-
.event_type ⇒ Symbol
Metaclass attribute, contains the key that is used for generating the specific transition via #make.
Instance Attribute Summary collapse
-
#destination_state ⇒ State
readonly
The state that the transition leads to.
-
#event_trigger_value ⇒ Object
readonly
A more specific object that triggers the transition.
-
#options ⇒ Hash
readonly
Configuration options of the transition.
-
#source_state ⇒ State
readonly
The state from which the transition starts.
-
#state_machine ⇒ Base
readonly
The state machine that this transition belongs to.
Class Method Summary collapse
-
.make(options) ⇒ Transition
Creates a new Transition object with the given options.
-
.types ⇒ Array<Class<Transition>>
A list of all registered transition subclasses.
Instance Method Summary collapse
-
#allowed? ⇒ Boolean
Asks the guard blocks given for
:if
and:unless
if the transition is allowed. -
#event_description ⇒ String
A short description of the event.
-
#initialize(options) ⇒ Transition
constructor
Initializes a new Transition between two given states.
- #inspect ⇒ Object
Constructor Details
#initialize(options) ⇒ Transition
This method should not be used directly. To create Transition
objects, use #make instead.
Initializes a new StateMachine::Transition between two given states.
Additionally, you must also supply an event trigger value as option. Its key must be equal to the transition class’s event_type
., ex. if event_type
is :on
, you have to supply the event value using the option key :on
.
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 |
# File 'lib/motion-state-machine/transition.rb', line 98 def initialize() @options = .dup @state_machine = @options.delete :state_machine @source_state = @state_machine.state [:from] @destination_state = @state_machine.state [:to] event_type = self.class.event_type if event_type.nil? raise RuntimeError, "#{self.class} has no defined event type." end [:from, :to].each do |symbol| unless [symbol].is_a?(Symbol) raise ":#{symbol} option must be given as symbol." end end @event_trigger_value = [event_type] if @event_trigger_value.nil? raise ArgumentError, "You must supply an event trigger value." end if [:internal] && [:from] != [:to] raise ArgumentError, "Internal states must have same source and destination state." end end |
Class Attribute Details
.event_type ⇒ Symbol
Returns Metaclass attribute, contains the key that is used for generating the specific transition via #make.
31 32 33 |
# File 'lib/motion-state-machine/transition.rb', line 31 def event_type @event_type end |
Instance Attribute Details
#destination_state ⇒ State (readonly)
Returns the state that the transition leads to.
21 22 23 |
# File 'lib/motion-state-machine/transition.rb', line 21 def destination_state @destination_state end |
#event_trigger_value ⇒ Object (readonly)
Returns a more specific object that triggers the transition.
25 26 27 |
# File 'lib/motion-state-machine/transition.rb', line 25 def event_trigger_value @event_trigger_value end |
#options ⇒ Hash (readonly)
Returns configuration options of the transition.
12 13 14 |
# File 'lib/motion-state-machine/transition.rb', line 12 def @options end |
#source_state ⇒ State (readonly)
Returns the state from which the transition starts.
18 19 20 |
# File 'lib/motion-state-machine/transition.rb', line 18 def source_state @source_state end |
#state_machine ⇒ Base (readonly)
Returns the state machine that this transition belongs to.
15 16 17 |
# File 'lib/motion-state-machine/transition.rb', line 15 def state_machine @state_machine end |
Class Method Details
.make(options) ⇒ Transition
Creates a new StateMachine::Transition object with the given options. The returned object’s subclass is determined by the :type
option.
See #initialize for all possible options.
56 57 58 59 |
# File 'lib/motion-state-machine/transition.rb', line 56 def self.make() klass = @@types_to_subclasses[[:type]] klass.new end |
.types ⇒ Array<Class<Transition>>
Returns a list of all registered transition subclasses.
38 39 40 |
# File 'lib/motion-state-machine/transition.rb', line 38 def self.types @@types_to_subclasses.keys end |
Instance Method Details
#allowed? ⇒ Boolean
Returns Asks the guard blocks given for :if
and :unless
if the transition is allowed. Returns true
if the transition is allowed to be executed.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/motion-state-machine/transition.rb', line 131 def allowed? if_guard = [:if] unless if_guard.nil? return false unless if_guard.call(@state_machine) end unless_guard = [:unless] unless unless_guard.nil? return false if unless_guard.call(@state_machine) end true end |
#event_description ⇒ String
Returns a short description of the event. Used for debug output.
147 148 149 150 |
# File 'lib/motion-state-machine/transition.rb', line 147 def event_description # Implement this in a subclass. "after unclassified event" end |
#inspect ⇒ Object
153 154 155 156 |
# File 'lib/motion-state-machine/transition.rb', line 153 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} "\ "#{event_description} @options=#{.inspect}>" end |