Class: FiniteMachine::TransitionBuilder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/finite_machine/transition_builder.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.

A class reponsible for building transition out of parsed states

Used internally by DSL to

Instance Method Summary collapse

Constructor Details

#initialize(machine, name, attributes = {}) ⇒ TransitionBuilder

Initialize a TransitionBuilder

Examples:

TransitionBuilder.new(machine, {})


21
22
23
24
25
26
27
28
# File 'lib/finite_machine/transition_builder.rb', line 21

def initialize(machine, name, attributes = {})
  @machine    = machine
  @name       = name
  @attributes = attributes

  @event_definition = EventDefinition.new(machine)
  @state_definition = StateDefinition.new(machine)
end

Instance Method Details

#call(transitions) ⇒ self

Converts user transitions into internal FiniteMachine::Transition representation

Examples:

transition_builder.call([:green, :yellow] => :red)

Parameters:

  • transitions (Hash[Symbol])

    The transitions to extract states from

Returns:

  • (self)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/finite_machine/transition_builder.rb', line 41

def call(transitions)
  StateParser.parse(transitions) do |from, to|
    transition = Transition.new(@machine.env.target, @name,
                                @attributes.merge(states: { from => to }))
    silent = @attributes.fetch(:silent, false)
    @machine.events_map.add(@name, transition)
    next unless @machine.auto_methods?

    unless @machine.respond_to?(@name)
      @event_definition.apply(@name, silent)
    end
    @state_definition.apply(from => to)
  end
  self
end