Class: Scorpion::DependencyMap

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#scorpionScorpion (readonly)

Returns the scorpion that created the map.

Returns:

  • (Scorpion)

    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.

Returns:

  • (Dependency)

    the dependency to be hunted for.

See Also:



94
95
96
# 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(&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.

Examples:


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

Returns:

  • (self)


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.

Parameters:

  • contract (Class, Module, Symbol)

    describing the desired behavior of the dependency.

Returns:

  • (Dependency)

    the dependency matching the attribute.



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.

Parameters:

  • contract (Class, Module, Symbol)

    describing the desired behavior of the dependency.

Returns:

  • (Dependency)

    the dependency to be hunted for.



86
87
88
# 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) ⇒ self

Replicates the dependency in other_map into this map.

Parameters:

Returns:

  • (self)


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

#resetObject

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.

Returns:

  • (Dependency)

    the dependency to be hunted for.



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