Class: Scorpion::DependencyMap
- Inherits:
-
Object
- Object
- Scorpion::DependencyMap
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/scorpion/dependency_map.rb
Overview
#chart available Dependency and #find them based on desired attributes.
Attributes collapse
-
#scorpion ⇒ Scorpion
readonly
The scorpion that created the map.
Instance Method Summary collapse
-
#capture(contract, **options, &builder) ⇒ Dependency
(also: #singleton)
Captures a single dependency and returns the same instance fore each request for the resource.
-
#chart(&block) ⇒ self
Chart the Dependency that this hunting map can #find.
-
#find(contract) ⇒ Dependency
Find Dependency that matches the requested
contract. -
#hunt_for(contract, **options, &builder) ⇒ Dependency
Define Dependency that can be found on this map by
contract. -
#initialize(scorpion) ⇒ DependencyMap
constructor
A new instance of DependencyMap.
-
#replicate_from(other_map) ⇒ self
Replicates the dependency in
other_mapinto this map. -
#reset ⇒ Object
Remove all dependency mappings.
-
#share(&block) ⇒ Dependency
Share dependencies defined within the block with all child scorpions.
Constructor Details
#initialize(scorpion) ⇒ DependencyMap
Returns a new instance of DependencyMap.
33 34 35 36 |
# File 'lib/scorpion/dependency_map.rb', line 33 def initialize( scorpion ) @scorpion = scorpion reset end |
Instance Attribute Details
#scorpion ⇒ Scorpion (readonly)
Returns the scorpion that created the map.
15 16 17 |
# File 'lib/scorpion/dependency_map.rb', line 15 def scorpion @scorpion end |
Instance Method Details
#capture(contract, **options, &builder) ⇒ Dependency Also known as: singleton
Captures a single dependency and returns the same instance fore each request for the resource.
94 95 96 |
# File 'lib/scorpion/dependency_map.rb', line 94 def capture( contract, **, &builder ) active_dependency_set.unshift Dependency::CapturedDependency.new( define_dependency( contract, , &builder ) ) # rubocop:disable Metrics/LineLength end |
#chart(&block) ⇒ self
Chart the Scorpion::Dependency that this hunting map can #find.
The block is executed in the context of DependencyMap if the block does not accept any arguments so that #hunt_for, #capture and #share can be called as methods.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/scorpion/dependency_map.rb', line 67 def chart( &block ) return unless block_given? if block.arity == 1 yield self else instance_eval &block end self end |
#find(contract) ⇒ Dependency
Find Scorpion::Dependency that matches the requested contract.
41 42 43 44 |
# File 'lib/scorpion/dependency_map.rb', line 41 def find( contract ) dependency_set.find { |p| p.satisfies?( contract ) } || shared_dependency_set.find { |p| p.satisfies?( contract ) } end |
#hunt_for(contract, **options, &builder) ⇒ Dependency
Define Scorpion::Dependency that can be found on this map by contract.
If a block is given, it will be used build the actual instances of the dependency for the Scorpion.
86 87 88 |
# File 'lib/scorpion/dependency_map.rb', line 86 def hunt_for( contract, **, &builder ) active_dependency_set.unshift define_dependency( contract, , &builder ) end |
#replicate_from(other_map) ⇒ self
Replicates the dependency in other_map into this map.
118 119 120 121 122 123 124 125 126 |
# File 'lib/scorpion/dependency_map.rb', line 118 def replicate_from( other_map ) other_map.each do |dependency| if replica = dependency.replicate dependency_set << replica end end self end |
#reset ⇒ Object
Remove all dependency mappings.
129 130 131 132 133 134 135 |
# File 'lib/scorpion/dependency_map.rb', line 129 def reset @dependency_set&.each &:release @shared_dependency_set&.each &:release @dependency_set = @active_dependency_set = [] @shared_dependency_set = [] end |
#share(&block) ⇒ Dependency
Share dependencies defined within the block with all child scorpions.
101 102 103 104 105 106 107 |
# File 'lib/scorpion/dependency_map.rb', line 101 def share( &block ) old_set = active_dependency_set @active_dependency_set = shared_dependency_set yield ensure @active_dependency_set = old_set end |