Class: FiniteMachine::Transition Private
- Inherits:
-
Object
- Object
- FiniteMachine::Transition
- Includes:
- Threadable
- Defined in:
- lib/finite_machine/transition.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class describing a transition associated with a given event
The Transition is created with the ‘event` helper.
Instance Method Summary collapse
-
#check_conditions(*args) ⇒ Boolean
private
Verify conditions returning true if all match, false otherwise.
-
#initialize(context, name, attrs = {}) ⇒ Transition
constructor
Initialize a Transition.
-
#inspect ⇒ String
Return string representation.
-
#make_conditions ⇒ Array[Callable]
private
Reduce conditions.
-
#matches?(from) ⇒ Boolean
Check if this transition matches from state.
-
#to_s ⇒ String
Return transition name.
-
#to_state(from) ⇒ Symbol
Find to state for this transition given the from state.
Constructor Details
#initialize(context, name, attrs = {}) ⇒ Transition
Initialize a Transition
48 49 50 51 52 53 54 55 56 |
# File 'lib/finite_machine/transition.rb', line 48 def initialize(context, name, attrs = {}) @context = context @name = name @states = attrs.fetch(:states, {}) @if = Array(attrs.fetch(:if, [])) @unless = Array(attrs.fetch(:unless, [])) @conditions = make_conditions freeze end |
Instance Method Details
#check_conditions(*args) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Verify conditions returning true if all match, false otherwise
76 77 78 79 80 |
# File 'lib/finite_machine/transition.rb', line 76 def check_conditions(*args) conditions.all? do |condition| condition.call(context, *args) end end |
#inspect ⇒ String
Return string representation
134 135 136 137 138 |
# File 'lib/finite_machine/transition.rb', line 134 def inspect transitions = @states.map { |from, to| "#{from} -> #{to}" }.join(", ") "<##{self.class} @name=#{@name}, @transitions=#{transitions}, " \ "@when=#{@conditions}>" end |
#make_conditions ⇒ Array[Callable]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reduce conditions
63 64 65 66 |
# File 'lib/finite_machine/transition.rb', line 63 def make_conditions @if.map { |c| Callable.new(c) } + @unless.map { |c| Callable.new(c).invert } end |
#matches?(from) ⇒ Boolean
Check if this transition matches from state
95 96 97 |
# File 'lib/finite_machine/transition.rb', line 95 def matches?(from) states.keys.any? { |state| [ANY_STATE, from].include?(state) } end |
#to_s ⇒ String
Return transition name
125 126 127 |
# File 'lib/finite_machine/transition.rb', line 125 def to_s @name.to_s end |
#to_state(from) ⇒ Symbol
Find to state for this transition given the from state
112 113 114 |
# File 'lib/finite_machine/transition.rb', line 112 def to_state(from) states[from] || states[ANY_STATE] end |