class Scorpion::Hunt

Captures state for a specific hunt so that constructor dependencies can be shared with child dependencies.

@example

class Service
  depend_on do
    options UserOptions
  end

  def initialize( user )
  end
end

class UserOptions

  depend_on do
    user User
  end
end

user    = User.find 123
service = scorpion.fetch Service, user
service.options.user # => user

Attributes

scorpion[R]

@!attribute @return [Scorpion] scorpion used to fetch uncaptured dependency.

trip[R]

@!attribute @return [Trip] the current hunting trip.

trips[R]

@!attribute @return [Array<Array>] the stack of trips conducted by the hunt to help

resolve child dependencies.

Public Class Methods

new( scorpion, contract, *arguments, &block ) click to toggle source

@!endgroup Attributes

# File lib/scorpion/hunt.rb, line 62
def initialize( scorpion, contract, *arguments, &block )
  @scorpion  = scorpion
  @trips     = []
  @trip      = Trip.new contract, arguments, block
end

Public Instance Methods

fetch( contract, *arguments, &block ) click to toggle source

Hunt for additional dependency to satisfy the main hunt's contract. @see Scorpion#hunt

# File lib/scorpion/hunt.rb, line 70
def fetch( contract, *arguments, &block )
  push contract, arguments, block
  execute
ensure
  pop
end
inject( object ) click to toggle source

Inject given `object` with its expected dependencies. @param [Scorpion::Object] object to be injected. @return [Scorpion::Object] the injected object.

# File lib/scorpion/hunt.rb, line 80
def inject( object )
  trip.object = object
  object.send :scorpion_hunt=, self

  object.injected_attributes.each do |attr|
    next if object.send "#{ attr.name }?"
    next if attr.lazy?

    object.send :inject_dependency, attr, fetch( attr.contract )
  end

  object.send :on_injected

  object
end
new( klass, *arguments, &block )
Alias for: spawn
spawn( klass, *arguments, &block ) click to toggle source

Allow the hunt to spawn objects. @see Scorpion#spawn

# File lib/scorpion/hunt.rb, line 98
def spawn( klass, *arguments, &block )
  scorpion.spawn( self, klass, *arguments, &block )
end
Also aliased as: new

Private Instance Methods

execute() click to toggle source
# File lib/scorpion/hunt.rb, line 105
def execute
  execute_from_trips || execute_from_scorpion
end
execute_from_scorpion() click to toggle source
# File lib/scorpion/hunt.rb, line 136
def execute_from_scorpion
  scorpion.execute self
end
execute_from_trip( trip ) click to toggle source
# File lib/scorpion/hunt.rb, line 119
def execute_from_trip( trip )
  return unless obj = trip.object
  return obj if contract === obj

  # If we have already resolved an instance of this contract in this
  # hunt, then return that same object.
  if obj.is_a? Scorpion::Object
    obj.injected_attributes.each do |attr|
      next unless attr.contract == contract

      return obj.send( attr.name ) if obj.send( :"#{ attr.name }?" )
    end
  end

  nil
end
execute_from_trips() click to toggle source
# File lib/scorpion/hunt.rb, line 109
def execute_from_trips
  trips.each do |trip|
    if resolved = execute_from_trip( trip )
      return resolved
    end
  end

  nil
end
pop() click to toggle source
# File lib/scorpion/hunt.rb, line 146
def pop
  @trip = trips.pop
end
push( contract, arguments, block ) click to toggle source
# File lib/scorpion/hunt.rb, line 140
def push( contract, arguments, block )
  trips.push trip

  @trip = Trip.new contract, arguments, block
end