Class: Scorpion::Hunt

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/scorpion/hunt.rb

Overview

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

Examples:


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

Defined Under Namespace

Classes: InitializerTrip, Trip

Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(scorpion, contract, *arguments, &block) ⇒ Hunt

Returns a new instance of Hunt.



62
63
64
65
66
# File 'lib/scorpion/hunt.rb', line 62

def initialize( scorpion, contract, *arguments, &block )
  @scorpion  = scorpion
  @trips     = []
  @trip      = Trip.new contract, arguments, block
end

Instance Attribute Details

#argumentsArray<Object> (readonly)

Returns positional arguments to pass to the initializer of #contract when found.

Returns:

  • (Array<Object>)

    positional arguments to pass to the initializer of #contract when found.



# File 'lib/scorpion/hunt.rb', line 53

#block#call

Returns block to pass to constructor of contract when found.

Returns:

  • (#call)

    block to pass to constructor of contract when found.



# File 'lib/scorpion/hunt.rb', line 56

#contractClass, ...

Returns contract being hunted for.

Returns:

  • (Class, Module, Symbol)

    contract being hunted for.



# File 'lib/scorpion/hunt.rb', line 50

#scorpionScorpion

Returns scorpion used to fetch uncaptured dependency.

Returns:

  • (Scorpion)

    scorpion used to fetch uncaptured dependency.



35
36
37
# File 'lib/scorpion/hunt.rb', line 35

def scorpion
  @scorpion
end

#trip=(value) ⇒ Trip

Returns the current hunting trip.

Returns:

  • (Trip)

    the current hunting trip.



45
46
47
# File 'lib/scorpion/hunt.rb', line 45

def trip
  @trip
end

#trips=(value) ⇒ Array<Array>

Returns the stack of trips conducted by the hunt to help resolve child dependencies.

Returns:

  • (Array<Array>)

    the stack of trips conducted by the hunt to help resolve child dependencies.



40
41
42
# File 'lib/scorpion/hunt.rb', line 40

def trips
  @trips
end

Instance Method Details

#fetch(contract, *arguments, &block) ⇒ Object

Hunt for additional dependency to satisfy the main hunt's contract.

See Also:

  • Scorpion#hunt


70
71
72
73
74
75
# File 'lib/scorpion/hunt.rb', line 70

def fetch( contract, *arguments, &block )
  push contract, arguments, block
  execute
ensure
  pop
end

#inject(object) ⇒ Scorpion::Object

Inject given object with its expected dependencies.

Parameters:

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

#spawn(klass, *arguments, &block) ⇒ Object Also known as: new

Allow the hunt to spawn objects.

See Also:



98
99
100
# File 'lib/scorpion/hunt.rb', line 98

def spawn( klass, *arguments, &block )
  scorpion.spawn( self, klass, *arguments, &block )
end