Sha256: 7c8b37bc1bab0843ac6bc906b7571fece2f1a80d60f2d7337404e6b50007b1ca

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

# encoding: utf-8

module FiniteMachine
  # A class responsible for building event methods
  class EventBuilder
    include Threadable
    include Safety

    # The current state machine
    attr_threadsafe :machine

    # Initialize an EventBuilder
    #
    # @param [FiniteMachine::StateMachine] machine
    #
    # @api private
    def initialize(machine)
      @machine = machine
    end

    # Build state machine events
    #
    # @param [FiniteMachine::Transition] transition
    #   the transition for which event is build
    #
    # @return [FiniteMachine::Transition]
    #
    # @api private
    def call(transition)
      name = transition.name
      detect_event_conflict!(name)
      if machine.singleton_class.send(:method_defined?, name)
        machine.events_chain.insert(name, transition)
      else
        define_event_transition(name, transition)
        define_event_bang(name)
      end
      transition
    end

    private

    # Define transition event
    #
    # @param [Symbol] name
    #   the event name
    #
    # @param [FiniteMachine::Transition] transition
    #   the transition this event is associated with
    #
    # @return [nil]
    #
    # @api private
    def define_event_transition(name, transition)
      silent = transition.silent
      _event = FiniteMachine::Event.new(machine, name: name, silent: silent)
      _event << transition
      machine.events_chain.add(name, _event)

      machine.send(:define_singleton_method, name) do |*args, &block|
        _event.call(*args, &block)
      end
    end

    # Define event that skips validations
    #
    # @param [Symbol] name
    #   the event name
    #
    # @return [nil]
    #
    # @api private
    def define_event_bang(name)
      machine.send(:define_singleton_method, "#{name}!") do
        transitions   = machine.transitions[name]
        machine.state = transitions.values[0]
      end
    end
  end # EventBuilder
end # FiniteMachine

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
finite_machine-0.10.1 lib/finite_machine/event_builder.rb
finite_machine-0.10.0 lib/finite_machine/event_builder.rb
finite_machine-0.9.2 lib/finite_machine/event_builder.rb
finite_machine-0.9.1 lib/finite_machine/event_builder.rb
finite_machine-0.9.0 lib/finite_machine/event_builder.rb
finite_machine-0.8.1 lib/finite_machine/event_builder.rb