class Scorpion::DependencyMap

{#chart} available {Dependency} and {#find} them based on desired {Scorpion::Attribute attributes}.

Attributes

active_dependency_set[R]

@return [Set] the active dependency set either {#dependency_set} or {#shared_dependency_set}

dependency_set[R]

@return [Set] the set of dependency charted on this map.

scorpion[R]

@return [Scorpion] the scorpion that created the map.

shared_dependency_set[R]

@return [Set] the set of dependencies charted on this map that is shared

with all child dependencies.

Public Class Methods

new( scorpion ) click to toggle source

@!endgroup Attributes

# File lib/scorpion/dependency_map.rb, line 33
def initialize( scorpion )
  @scorpion              = scorpion
  reset
end

Public Instance Methods

capture( contract, **options, &builder ) click to toggle source

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
Also aliased as: singleton
chart( ) { |self| ... } click to toggle source

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
each( &block ) click to toggle source

@visibility private

# File lib/scorpion/dependency_map.rb, line 110
def each( &block )
  dependency_set.each &block
end
find( contract ) click to toggle source

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
hunt_for( contract, **options, &builder ) click to toggle source

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
replicate_from( other_map ) click to toggle source

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
reset() click to toggle source

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
share( ) { || ... } click to toggle source

Share dependencies defined within the block with all child scorpions. @return [Dependency] the dependency to be hunted for.

# 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
singleton( contract, **options, &builder )
Alias for: capture

Private Instance Methods

define_dependency( contract, options, &builder ) click to toggle source
# File lib/scorpion/dependency_map.rb, line 139
def define_dependency( contract, options, &builder )
  Dependency.define contract, options, &builder
end