Class: FiniteMachine::Transition Private

Inherits:
Object
  • Object
show all
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.

Examples:

Converting event into Transition

event :go, :red => :green

will be translated to

Transition.new(context, :go, {states: {:red => :green}})

Instance Method Summary collapse

Constructor Details

#initialize(context, name, attrs = {}) ⇒ Transition

Initialize a Transition

Examples:

attributes = {states: {green: :yellow}}
Transition.new(context, :go, attributes)

Parameters:

  • context (Object)

    the context this transition evaluets conditions in

  • attrs (Hash) (defaults to: {})


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

Parameters:

  • args (Array[Object])

    the arguments for the condition

Returns:

  • (Boolean)


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

#inspectString

Return string representation

Returns:

  • (String)


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_conditionsArray[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

Returns:



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

Examples:

transition = Transition.new(context, states: {:green => :red})
transition.matches?(:green) # => true

Parameters:

  • from (Symbol)

    the from state to match against

Returns:

  • (Boolean)

    Return true if match is found, false otherwise.



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_sString

Return transition name

Examples:

transition = Transition.new(context, name: :go)
transition.to_s # => "go"

Returns:

  • (String)


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

Examples:

transition = Transition.new(context, states: {:green => :red})
transition.to_state(:green) # => :red

Parameters:

  • from (Symbol)

    the from state to check

Returns:

  • (Symbol)

    the to state



112
113
114
# File 'lib/finite_machine/transition.rb', line 112

def to_state(from)
  states[from] || states[ANY_STATE]
end