class Scorpion::DependencyMap
{#chart} available {Dependency} and {#find} them based on desired {Scorpion::Attribute attributes}.
Attributes
@return [Set] the active dependency set either {#dependency_set} or {#shared_dependency_set}
@return [Set] the set of dependency charted on this map.
@return [Scorpion] the scorpion that created the map.
Public Class Methods
@!endgroup Attributes
# File lib/scorpion/dependency_map.rb, line 33 def initialize( scorpion ) @scorpion = scorpion reset end
Public Instance Methods
Captures a single dependency and returns the same instance fore each request for the resource. @see hunt_for
@return [Dependency] the dependency to be hunted for.
# File lib/scorpion/dependency_map.rb, line 94 def capture( contract, **options, &builder ) active_dependency_set.unshift Dependency::CapturedDependency.new( define_dependency( contract, options, &builder ) ) # rubocop:disable Metrics/LineLength end
Chart the {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.
@example
cache = {} chart do self #=> DependencyMap hunt_for Repository capture Cache, return: cache # => NoMethodError end chart do |map| map.hunt_for Repository map.capture Cache, return: cache # => No problem end
@return [self]
# 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
@visibility private
# File lib/scorpion/dependency_map.rb, line 110 def each( &block ) dependency_set.each &block end
Find {Dependency} that matches the requested `contract`. @param [Class,Module,Symbol] contract describing the desired behavior of the dependency. @return [Dependency] the dependency matching the attribute.
# 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
Define {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}.
@param [Class,Module,Symbol] contract describing the desired behavior of the dependency. @return [Dependency] the dependency to be hunted for.
# File lib/scorpion/dependency_map.rb, line 86 def hunt_for( contract, **options, &builder ) active_dependency_set.unshift define_dependency( contract, options, &builder ) end
Replicates the dependency in `other_map` into this map. @param [Scorpion::DependencyMap] other_map to replicate from. @return [self]
# 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
Remove all dependency mappings.
# 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
Private Instance Methods
# File lib/scorpion/dependency_map.rb, line 139 def define_dependency( contract, options, &builder ) Dependency.define contract, options, &builder end