Sha256: cc71b41c83c885714b37df0a16c4200100b144b21d7beb70b605f1745169d1e0

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# encoding: utf-8

module FiniteMachine
  # A class responsible for defining standalone state machine
  class Definition
    # The machine deferreds
    #
    # @return [Array[Proc]]
    #
    # @api private
    def self.deferreds
      @deferreds ||= []
    end

    # Add deferred
    #
    # @param [Proc] deferred
    #   the deferred execution
    #
    # @return [Array[Proc]]
    #
    # @api private
    def self.add_deferred(deferred)
      deferreds << deferred
    end

    # Instantiate a new Definition
    #
    # @example
    #   class Engine < FiniteMachine::Definition
    #     ...
    #   end
    #
    #   engine = Engine.new
    #
    # @return [FiniteMachine::StateMachine]
    #
    # @api public
    def self.new(*args)
      context = self
      FiniteMachine.define(*args) do
        context.deferreds.each { |d| d.call(self) }
      end
    end

    # Delay lookup of DSL method
    #
    # @param [Symbol] method_name
    #
    # @return [nil]
    #
    # @api private
    def self.method_missing(method_name, *arguments, &block)
      deferred = proc do |name, args, blok, object|
        object.send(name, *args, &blok)
      end
      deferred = deferred.curry(4)[method_name][arguments][block]
      add_deferred(deferred)
    end
  end # Definition
end # FiniteMachine

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
finite_machine-0.10.0 lib/finite_machine/definition.rb
finite_machine-0.9.2 lib/finite_machine/definition.rb
finite_machine-0.9.1 lib/finite_machine/definition.rb
finite_machine-0.9.0 lib/finite_machine/definition.rb