Sha256: 11e305b62ae0be72483ea662b686d37a0337dfd9d001be854beba16a804bee1b

Contents?: true

Size: 1.79 KB

Versions: 10

Compression:

Stored size: 1.79 KB

Contents

module Interactor
  # Public: Interactor::Organizer methods. Because Interactor::Organizer is a
  # module, custom Interactor::Organizer classes should include
  # Interactor::Organizer rather than inherit from it.
  #
  # Examples
  #
  #   class MyOrganizer
  #     include Interactor::Organizer
  #
  #     organizer InteractorOne, InteractorTwo
  #   end
  module Organizer
    extend ActiveSupport::Concern
    include Interactor

    # Internal: Interactor::Organizer class methods.
    class_methods do
      # Public: Declare Interactors to be invoked as part of the
      # Interactor::Organizer's invocation. These interactors are invoked in
      # the order in which they are declared.
      #
      # interactors - Zero or more (or an Array of) Interactor classes.
      #
      # Examples
      #
      #   class MyFirstOrganizer
      #     include Interactor::Organizer
      #
      #     organize InteractorOne, InteractorTwo
      #   end
      #
      #   class MySecondOrganizer
      #     include Interactor::Organizer
      #
      #     organize [InteractorThree, InteractorFour]
      #   end
      #
      # Returns nothing.
      def organize(*interactors)
        @organized = interactors.flatten
      end

      # Internal: An Array of declared Interactors to be invoked.
      #
      # Examples
      #
      #   class MyOrganizer
      #     include Interactor::Organizer
      #
      #     organize InteractorOne, InteractorTwo
      #   end
      #
      #   MyOrganizer.organized
      #   # => [InteractorOne, InteractorTwo]
      #
      # Returns an Array of Interactor classes or an empty Array.
      def organized
        @organized ||= []
      end
    end

    def call
      self.class.organized.inject(context) do |ctx, interactor|
        interactor.call!(ctx)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
interactor_with_steroids-1.5.1 lib/interactor/organizer.rb
interactor_with_steroids-1.5.0 lib/interactor/organizer.rb
interactor_with_steroids-1.4.0 lib/interactor/organizer.rb
interactor_with_steroids-1.3.0 lib/interactor/organizer.rb
interactor_with_steroids-1.2.0 lib/interactor/organizer.rb
interactor_with_steroids-1.1.2 lib/interactor/organizer.rb
interactor_with_steroids-1.1.1 lib/interactor/organizer.rb
interactor_with_steroids-1.1.0 lib/interactor/organizer.rb
interactor_with_steroids-1.0.0 lib/interactor/organizer.rb
interactor_with_steroids-0.0.1 lib/interactor/organizer.rb