Sha256: d9dcc2c923c1e06c1e3b61e18ea032d151c0018895d39af590ec2a0817daf5d0

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module Interaktor::Organizer
  # When the Interaktor::Organizer module is included in a class, add the
  # relevant class methods and hooks to that class.
  #
  # @param base [Class] the class which is including the Interaktor::Organizer
  #   module
  def self.included(base)
    base.class_eval do
      include Interaktor

      extend ClassMethods
      include InstanceMethods
    end
  end

  module ClassMethods
    # Declare Interaktors to be invoked as part of the Interaktor::Organizer's
    # invocation. These interaktors are invoked in the order in which they are
    # declared.
    #
    # @param interaktors [Interaktor, Array<Interaktor>]
    #
    # @return [void]
    def organize(*interaktors)
      organized.concat(interaktors.flatten)
    end

    # An array of declared Interaktors to be invoked.
    #
    # @return [Array<Interaktor>]
    def organized
      @organized ||= []
    end
  end

  module InstanceMethods
    # Invoke the organized Interaktors. An Interaktor::Organizer is expected
    # NOT to define its own `#call` in favor of this default implementation.
    #
    # @return [void]
    def call
      self.class.organized.each do |interaktor|
        interaktor.call!(context)
      end
    end

    private

    # A list of attributes required to invoke the organized Interaktors.
    # Obtained by compiling a list of all required attributes of all organized
    # interaktors. Duplicates are removed.
    #
    # @return [Array<Symbol>]
    def required_attributes
      self.class.organized.map(&:required_attributes).flatten.uniq
    end

    # A list of optional attributes allowed to be included when invoking the
    # organized Interaktors. Obtained by compiling a list of all optional
    # attributes of all organized interaktors. Duplicates are removed.
    #
    # @return [Array<Symbol>]
    def optional_attributes
      self.class.organized.map(&:optional_attributes).flatten.uniq
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
interaktor-0.1.3 lib/interaktor/organizer.rb