Sha256: 2132702006c91821519349a420bdc13763310bd100eccca28044902e29e3884b

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

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.new(*args) do
        context.deferreds.each { |d| d.call(self) }
      end
    end

    # Set deferrerd methods on the subclass
    #
    # @api private
    def self.inherited(subclass)
      super

      self.deferreds.each { |d| subclass.add_deferred(d) }
    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

3 entries across 3 versions & 1 rubygems

Version Path
finite_machine-0.13.0 lib/finite_machine/definition.rb
finite_machine-0.12.1 lib/finite_machine/definition.rb
finite_machine-0.12.0 lib/finite_machine/definition.rb