Sha256: ba3611509ad093c4b5a85dc33b5e44bfc9f26e99fde709f583432e7a7d2ecf59

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Coactive
  module Lookups
    class Object < Base
      def call
        lookup(@coactant)
      end

      private

      def lookup(coactant)
        return if terminate?(coactant)

        if coactant.name.present? && (coactor = resolve(coactant))
          coactor
        elsif @config.lookup_superclass_for_object && coactant.superclass
          lookup(coactant.superclass)
        end
      end

      def terminate?(coactant)
        coactant.name.to_s.in?(@config.lookup_superclass_until)
      end

      def resolve(coactant)
        name = resolve_name(coactant)
        coactor = name.safe_constantize
        return coactor if coactor && name == coactor.name
      end

      def resolve_name(coactant)
        suffix = @config.class_suffix
        namespace = @klass.name.to_s.sub(/#{suffix}$/, '').to_s
        [namespace, "#{coactant.name}#{suffix}"].join('::')
      end

      class << self
        def callable?(coactant)
          coactant.is_a?(Module)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
coactive-0.6.0 lib/coactive/lookups/object.rb
coactive-0.5.2 lib/coactive/lookups/object.rb
coactive-0.5.1 lib/coactive/lookups/object.rb
coactive-0.5.0 lib/coactive/lookups/object.rb
coactive-0.4.1 lib/coactive/lookups/object.rb
coactive-0.4.0 lib/coactive/lookups/object.rb