Sha256: e35fb0fbd68818c97afb01de78a28453a998d290bd2e35775c3275645d6aa10d

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require_relative 'lookups/base'
require_relative 'lookups/name'
require_relative 'lookups/object'

module IIInteractor
  module Lookup
    extend ActiveSupport::Concern

    def lookup_all
      lookup.map { |interactor| [interactor] + interactor.new(@context).lookup_all }.flatten
    end

    def lookup
      self.class._interactions.map do |interaction|
        if interaction.is_a?(Symbol) && respond_to?(interaction, true)
          send(interaction)
        elsif interaction.is_a?(Proc)
          instance_exec(&interaction)
        else
          interaction
        end
      end.flatten.compact.map do |interaction|
        if interaction.is_a?(Class) && interaction < IIInteractor::Base
          interaction
        else
          self.class.lookup(interaction)
        end
      end.flatten.compact
    end

    class_methods do
      def lookup(*interactions)
        interactions = _interactions unless interactions
        interactions.map { |interaction| Lookup.call(self, interaction) }.flatten
      end
    end

    class << self
      class_attribute :lookups
      self.lookups = [Lookups::Name, Lookups::Object]

      class_attribute :cache
      self.cache = {}

      def call(klass, interaction)
        with_cache(klass, interaction) do
          lookup = lookups.detect { |lookup| lookup.call?(interaction) }
          lookup.new(klass, interaction).call if lookup
        end
      end

      private

      def with_cache(klass, interaction)
        if Config.lookup_cache
          self.cache[klass] ||= {}
          self.cache[klass][interaction] ||= yield
        else
          yield
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ii_interactor-1.2.0 lib/ii_interactor/lookup.rb
ii_interactor-1.1.1 lib/ii_interactor/lookup.rb
ii_interactor-1.1.0 lib/ii_interactor/lookup.rb