module Scorpion

Constants

VERSION
VERSION_NUMBER
VERSION_SUFFIX

Public Class Methods

fetch( dependencies, &block ) click to toggle source

Hunt for dependency from the primary Scorpion {#instance}. @see fetch

# File lib/scorpion.rb, line 139
def self.fetch( dependencies, &block )
  instance.fetch dependencies, &block
end
instance() click to toggle source

@!attribute instance @return [Scorpion] main scorpion for the app.

# File lib/scorpion.rb, line 111
def self.instance
  @instance_referenced = true
  @instance
end
instance=( scorpion ) click to toggle source
# File lib/scorpion.rb, line 118
def self.instance=( scorpion )
  if @instance_referenced
    logger.warn "Replacing the global Scorpion.instance will not update any Scorpion::Nest instances created with the original scorpion." if warn_global_replace # rubocop:disable Metrics/LineLength
    @instance_referenced = false
  end
  @instance = scorpion
end
logger() click to toggle source

@!attribute logger @return [Logger] logger for the Scorpion framework to use.

# File lib/scorpion.rb, line 145
def self.logger
  @logger ||= defined?( ::Rails ) ? ::Rails.logger : Logger.new( STDOUT )
end
logger=( value ) click to toggle source
# File lib/scorpion.rb, line 149
def self.logger=( value )
  @logger = value
end
prepare( reset = false, &block ) click to toggle source

Prepare the {#instance} for hunting. @param [Boolean] reset true to free all existing resource and initialize a

new scorpion.
# File lib/scorpion.rb, line 132
def self.prepare( reset = false, &block )
  @instance.reset if reset
  instance.prepare &block
end

Public Instance Methods

build_nest() click to toggle source

@return [Scorpion::Nest] a nest that uses this scorpion as the mother.

# File lib/scorpion.rb, line 87
def build_nest
  Scorpion::Nest.new( self )
end
destroy() click to toggle source

Free up any captured dependency and release any long-held resources.

# File lib/scorpion.rb, line 78
def destroy
  reset
end
execute( hunt ) click to toggle source

Execute the `hunt` returning the desired dependency. @param [Hunt] hunt to execute. @return [Object] an object that satisfies the hunt contract.

# File lib/scorpion.rb, line 66
def execute( hunt )
  fail "Not implemented"
end
fetch( contract, *arguments, &block ) click to toggle source

Hunts for an object that satisfies the requested `contract`.

@param [Class,Module,Symbol] contract describing the desired behavior of the dependency. @return [Object] an object that satisfies the contract. @raise [UnsuccessfulHunt] if a matching object cannot be found.

# File lib/scorpion.rb, line 26
def fetch( contract, *arguments, &block )
  hunt = Hunt.new( self, contract, *arguments, &block )
  execute hunt
end
inject( target ) click to toggle source

Inject the {#target} with all non-lazy dependencies. @param [Scorpion::Object] target to inject. @return [target]

# File lib/scorpion.rb, line 47
def inject( target )
  hunt = Scorpion::Hunt.new self, nil
  hunt.inject target

  target
end
logger() click to toggle source

@!attribute @return [Logger] logger for the scorpion to use.

# File lib/scorpion.rb, line 93
def logger
  @logger || Scorpion.logger
end
logger=( value ) click to toggle source
# File lib/scorpion.rb, line 97
def logger=( value )
  @logger = value
end
new( object_class, *arguments, &block ) click to toggle source

Explicitly spawn an instance of {#object_class} and inject it's dependencies. @param [Array<Object>] args to pass to the constructor. @param [#call] block to pass to the constructor. @return [Scorpion::Object] the spawned object.

# File lib/scorpion.rb, line 58
def new( object_class, *arguments, &block )
  hunt = Hunt.new( self, object_class, *arguments, &block )
  Scorpion::Dependency::ClassDependency.new( object_class ).fetch( hunt )
end
replicate( &block ) click to toggle source

Creates a new {Scorpion} copying the current configuration any any currently captured dependency. @return [Scorpion] the replicated scorpion.

# File lib/scorpion.rb, line 73
def replicate( &block )
  fail "Not implemented"
end
reset() click to toggle source

Reset the hunting map and clear all dependencies.

# File lib/scorpion.rb, line 83
def reset
end
spawn( hunt, object_class, *arguments, &block ) click to toggle source

Creates a new object and feeds it it's dependencies. @param [Class] object_class a class that includes {Scorpion::Object}. @param [Array<Object>] args to pass to the constructor. @param [#call] block to pass to the constructor. @return [Scorpion::Object] the spawned object.

# File lib/scorpion.rb, line 36
def spawn( hunt, object_class, *arguments, &block )
  if object_class < Scorpion::Object
    object_class.spawn hunt, *arguments, &block
  else
    object_class.new *arguments, &block
  end
end

Private Instance Methods

unsuccessful_hunt( contract ) click to toggle source

Used by concrete scorpions to notify the caller that the hunt was unsuccessful.

# File lib/scorpion.rb, line 161
def unsuccessful_hunt( contract )
  fail UnsuccessfulHunt, contract
end