Sha256: a45cb2d2a6450b88bbefa03c83e8955c0a513969120f9a9e62002808de5df81d

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

module Scorpion
  # A concrete implementation of a Scorpion used to hunt down food for a {Scorpion::Object}.
  # @see Scorpion
  class Hunter
    include Scorpion

    # ============================================================================
    # @!group Attributes
    #

    # @return [Scorpion::DependencyMap] map of {Dependency} and how to create instances.
      attr_reader :dependency_map
      protected :dependency_map

    # @return [Scorpion] parent scorpion to deferr hunting to on missing dependency.
      attr_reader :parent
      private :parent

    #
    # @!endgroup Attributes

    def initialize( parent = nil, &block )
      @parent      = parent
      @dependency_map = Scorpion::DependencyMap.new( self )

      prepare &block if block_given?
    end

    # Prepare the scorpion for hunting.
    # @see DependencyMap#chart
    def prepare( &block )
      dependency_map.chart &block
    end

    # Expose dependency injection definitions as top-level methods.
    [:hunt_for,:capture,:share].each do |delegate|
      define_method delegate do |*args,&block|
        prepare do |hunter|
          hunter.send delegate, *args, &block
        end
      end
    end

    # @see Scorpion#replicate
    def replicate
      replica = self.class.new self
      replica.dependency_map.replicate_from( dependency_map )
      replica
    end

    # @see Scorpion#hunt
    def execute( hunt, explicit_only = false )
      dependency   = dependency_map.find( hunt.contract, hunt.traits )
      dependency ||= parent.dependency_map.find( hunt.contract, hunt.traits ) if parent
      dependency ||= Dependency.define( hunt.contract ) if hunt.traits.blank? && !explicit_only

      unsuccessful_hunt( hunt.contract, hunt.traits ) unless dependency

      dependency.fetch hunt
    end

    # @see Scorpion#reset
    def reset
      dependency_map.reset
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scorpion-ioc-0.5.5 lib/scorpion/hunter.rb
scorpion-ioc-0.5.4 lib/scorpion/hunter.rb
scorpion-ioc-0.5.2 lib/scorpion/hunter.rb
scorpion-ioc-0.5.1 lib/scorpion/hunter.rb
scorpion-ioc-0.5.0 lib/scorpion/hunter.rb