Class: StateMachine::Transition Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-state-machine/transition.rb

Overview

This class is abstract.

Subclass and override #event_description, #arm and #unarm to implement a custom Transition class.

Constant Summary collapse

@@types_to_subclasses =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transition

Note:

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.

Options Hash (options):

  • :state_machine (StateMachine::Base)

    State machine that the transition belongs to.

  • :from (Symbol)

    State where the transition begins.

  • :to (Symbol)

    State where the transition ends.

  • :if (Proc) — default: nil

    Block that should return a Boolean. If the block returns false, the transition will be blocked and not executed (optional).

  • :unless (Proc) — default: nil

    Block that should return a Boolean. If the block returns true, the transition will be blocked and not executed (optional).

  • :internal (Boolean) — default: false

    If set to true, the transition will not leave it's source state: Entry and Exit actions will not be called in this case. For internal transitions, :from and :to must be the same (optional).


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)
  @options = options.dup
  @state_machine = @options.delete :state_machine
  @source_state = @state_machine.state options[:from]
  @destination_state = @state_machine.state options[: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 options[symbol].is_a?(Symbol)
      raise ":#{symbol} option must be given as symbol."
    end
  end

  @event_trigger_value = options[event_type]
  if @event_trigger_value.nil?
    raise ArgumentError, "You must supply an event trigger value."
  end

  if options[:internal] && options[:from] != options[:to]
    raise ArgumentError,
      "Internal states must have same source and destination state."
  end
end

Class Attribute Details

.event_typeSymbol


31
32
33
# File 'lib/motion-state-machine/transition.rb', line 31

def event_type
  @event_type
end

Instance Attribute Details

#destination_stateState (readonly)


21
22
23
# File 'lib/motion-state-machine/transition.rb', line 21

def destination_state
  @destination_state
end

#event_trigger_valueObject (readonly)


25
26
27
# File 'lib/motion-state-machine/transition.rb', line 25

def event_trigger_value
  @event_trigger_value
end

#optionsHash (readonly)


12
13
14
# File 'lib/motion-state-machine/transition.rb', line 12

def options
  @options
end

#source_stateState (readonly)


18
19
20
# File 'lib/motion-state-machine/transition.rb', line 18

def source_state
  @source_state
end

#state_machineBase (readonly)


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.

Examples:

StateMachine::Transition.make type: :to, ... # => #<StateMachine::Transition:...>

Options Hash (options):

  • :type (Symbol)

    Type identifier for the transition, ex. :on, :after, :on_notification.


56
57
58
59
# File 'lib/motion-state-machine/transition.rb', line 56

def self.make(options)
  klass = @@types_to_subclasses[options[:type]]
  klass.new options
end

.typesArray<Class<Transition>>


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


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/motion-state-machine/transition.rb', line 131

def allowed?
  if_guard = options[:if]
  unless if_guard.nil?
    return false unless if_guard.call(@state_machine)
  end
  unless_guard = options[:unless]
  unless unless_guard.nil?
    return false if unless_guard.call(@state_machine)
  end
  true
end

#event_descriptionString


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

#inspectObject


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=#{options.inspect}>"
end