Class: Scorpion::Hunter

Inherits:
Object
  • Object
show all
Includes:
Scorpion
Defined in:
lib/scorpion/hunter.rb

Overview

A concrete implementation of a Scorpion used to hunt down food for a Object.

See Also:

Constant Summary

Constants included from Scorpion

VERSION, VERSION_NUMBER, VERSION_SUFFIX

Instance Attribute Summary

Attributes included from Scorpion

#instance, #logger

Instance Method Summary collapse

Methods included from Scorpion

#build_nest, #destroy, #fetch, fetch, #inject, instance, instance=, logger, logger=, #new, prepare, #spawn

Constructor Details

#initialize(parent = nil, &block) ⇒ Hunter

Returns a new instance of Hunter.



22
23
24
25
26
27
# File 'lib/scorpion/hunter.rb', line 22

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

  prepare &block if block_given?
end

Instance Method Details

#execute(hunt, explicit_only = false) ⇒ Object

See Also:



52
53
54
55
56
57
58
59
# File 'lib/scorpion/hunter.rb', line 52

def execute( hunt, explicit_only = false )
  dependency   = find_dependency( hunt )
  dependency ||= Dependency.define( hunt.contract ) unless explicit_only

  unsuccessful_hunt( hunt.contract ) unless dependency

  dependency.fetch hunt
end

#find_dependency(hunt) ⇒ Dependency

Find any explicitly defined dependencies that can satisfy the hunt.

Parameters:

  • hunt (Hunt)

    being resolved.

Returns:

  • (Dependency)

    the matching dependency if found



64
65
66
67
68
69
# File 'lib/scorpion/hunter.rb', line 64

def find_dependency( hunt )
  dependency   = dependency_map.find( hunt.contract )
  dependency ||= parent.find_dependency( hunt ) if parent

  dependency
end

#inspectString

Returns:

  • (String)


77
78
79
80
81
82
83
# File 'lib/scorpion/hunter.rb', line 77

def inspect
  dependencies = dependency_map.to_a
  result = "<#{ self.class.name } contracts=#{ dependencies.inspect }"
  result << " parent=#{ parent.inspect }" if parent
  result << ">"
  result
end

#prepare(&block) ⇒ Object

Prepare the scorpion for hunting.



31
32
33
# File 'lib/scorpion/hunter.rb', line 31

def prepare( &block )
  dependency_map.chart &block
end

#replicateObject

See Also:



45
46
47
48
49
# File 'lib/scorpion/hunter.rb', line 45

def replicate
  replica = self.class.new self
  replica.dependency_map.replicate_from( dependency_map )
  replica
end

#resetObject

See Also:



72
73
74
# File 'lib/scorpion/hunter.rb', line 72

def reset
  dependency_map.reset
end