module Scorpion
Constants
- VERSION
- VERSION_NUMBER
- VERSION_SUFFIX
Public Class Methods
@!attribute instance @return [Scorpion] main scorpion for the app.
# File lib/scorpion.rb, line 111 def self.instance @instance_referenced = true @instance end
# 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
@!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
# File lib/scorpion.rb, line 149 def self.logger=( value ) @logger = value end
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
@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
Free up any captured dependency and release any long-held resources.
# File lib/scorpion.rb, line 78 def destroy reset end
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
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 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
@!attribute @return [Logger] logger for the scorpion to use.
# File lib/scorpion.rb, line 93 def logger @logger || Scorpion.logger end
# File lib/scorpion.rb, line 97 def logger=( value ) @logger = value end
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
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 the hunting map and clear all dependencies.
# File lib/scorpion.rb, line 83 def reset end
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
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