Sha256: 7c63d75625fb238dc9131f2c6da7c7ae3adb318754c223dde071c4278bdd6911

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# encoding: utf-8

require 'finite_machine/state_parser'
require 'finite_machine/event_definition'
require 'finite_machine/state_definition'

module FiniteMachine
  # A class reponsible for building transition out of parsed states
  #
  # Used internally by {DSL} to
  #
  # @api private
  class TransitionBuilder
    include Threadable

    # The current state machine
    attr_threadsafe :machine

    attr_threadsafe :attributes

    attr_threadsafe :event_definition

    attr_threadsafe :state_definition

    # Initialize a TransitionBuilder
    #
    # @example
    #   TransitionBuilder.new(machine, {})
    #
    # @api public
    def initialize(machine, attributes = {})
      @machine = machine
      @attributes = attributes
      @event_definition = EventDefinition.new(machine)
      @state_definition = StateDefinition.new(machine)
    end

    # Creates transitions for the states
    #
    # @example
    #   transition_builder.call([:green, :yellow] => :red)
    #
    # @param [Hash[Symbol]] states
    #   The states to extract
    #
    # @return [self]
    #
    # @api public
    def call(states)
      StateParser.new(states).parse do |from, to|
        attributes.merge!(states: { from => to })
        transition = Transition.new(machine, attributes)
        name = attributes[:name]
        silent = attributes.fetch(:silent, false)

        machine.events_chain.add(name, transition)

        unless machine.respond_to?(name)
          event_definition.apply(name, silent)
        end
        state_definition.apply({ from => to })
      end
      self
    end
  end # TransitionBuilder
end # FiniteMachine

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
finite_machine-0.11.3 lib/finite_machine/transition_builder.rb
finite_machine-0.11.2 lib/finite_machine/transition_builder.rb
finite_machine-0.11.1 lib/finite_machine/transition_builder.rb
finite_machine-0.11.0 lib/finite_machine/transition_builder.rb