Module: Scorpion

Included in:
ChainHunter, Hunter
Defined in:
lib/scorpion.rb,
lib/scorpion/hunt.rb,
lib/scorpion/nest.rb,
lib/scorpion/error.rb,
lib/scorpion/rails.rb,
lib/scorpion/rspec.rb,
lib/scorpion/hunter.rb,
lib/scorpion/method.rb,
lib/scorpion/object.rb,
lib/scorpion/stinger.rb,
lib/scorpion/version.rb,
lib/scorpion/attribute.rb,
lib/scorpion/rails/job.rb,
lib/scorpion/dependency.rb,
lib/scorpion/rails/nest.rb,
lib/scorpion/chain_hunter.rb,
lib/scorpion/rails/mailer.rb,
lib/scorpion/rspec/helper.rb,
lib/scorpion/attribute_set.rb,
lib/scorpion/rails/railtie.rb,
lib/scorpion/dependency_map.rb,
lib/scorpion/rails/controller.rb,
lib/scorpion/object_constructor.rb,
lib/scorpion/rails/active_record.rb,
lib/scorpion/rails/active_record/model.rb,
lib/scorpion/dependency/class_dependency.rb,
lib/scorpion/dependency/module_dependency.rb,
lib/scorpion/rails/active_record/relation.rb,
lib/scorpion/dependency/builder_dependency.rb,
lib/scorpion/dependency/argument_dependency.rb,
lib/scorpion/dependency/captured_dependency.rb,
lib/scorpion/rails/active_record/association.rb

Defined Under Namespace

Modules: Method, Object, Rails, Rspec, Stinger Classes: ArityMismatch, Attribute, AttributeSet, BuilderRequiredError, ChainHunter, Dependency, DependencyMap, Error, Hunt, Hunter, Nest, ObjectConstructor, UnsuccessfulHunt

Constant Summary collapse

VERSION_NUMBER =
"0.5.9"
VERSION_SUFFIX =
""
VERSION =
"#{VERSION_NUMBER}#{VERSION_SUFFIX}"

Convenience Methods collapse

Convenience Methods collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerLogger

Returns logger for the Scorpion framework to use.

Returns:

  • (Logger)

    logger for the Scorpion framework to use.



102
103
104
# File 'lib/scorpion.rb', line 102

def logger
  @logger || Scorpion.logger
end

Class Method Details

.fetch(dependencies, &block) ⇒ Object

Hunt for dependency from the primary Scorpion #instance.

See Also:



133
134
135
# File 'lib/scorpion.rb', line 133

def self.fetch( dependencies, &block )
  instance.fetch dependencies, &block
end

.fetch_by_traits(dependencies, &block) ⇒ Object

Hunt for dependency from the primary Scorpion #instance.

See Also:



139
140
141
# File 'lib/scorpion.rb', line 139

def self.fetch_by_traits( dependencies, &block )
  instance.fetch_by_traits dependencies, &block
end

.instanceScorpion

Returns main scorpion for the app.

Returns:

  • (Scorpion)

    main scorpion for the app.



118
119
120
# File 'lib/scorpion.rb', line 118

def self.instance
  @instance
end

.loggerLogger

Returns logger for the Scorpion framework to use.

Returns:

  • (Logger)

    logger for the Scorpion framework to use.



145
146
147
# File 'lib/scorpion.rb', line 145

def self.logger
  @logger ||= defined?( ::Rails ) ? ::Rails.logger : Logger.new( STDOUT )
end

.logger=(value) ⇒ Object



148
149
150
# File 'lib/scorpion.rb', line 148

def self.logger=( value )
  @logger = value
end

.prepare(reset = false, &block) ⇒ Object

Prepare the #instance for hunting.

Parameters:

  • reset (Boolean) (defaults to: false)

    true to free all existing resource and initialize a new scorpion.



126
127
128
129
# File 'lib/scorpion.rb', line 126

def self.prepare( reset = false, &block )
  @instance.reset if reset
  instance.prepare &block
end

Instance Method Details

#build_nestScorpion::Nest

Returns a nest that uses this scorpion as the mother.

Returns:



96
97
98
# File 'lib/scorpion.rb', line 96

def build_nest
  Scorpion::Nest.new( self )
end

#destroyObject

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



87
88
89
# File 'lib/scorpion.rb', line 87

def destroy
  reset
end

#execute(hunt) ⇒ Object

Execute the hunt returning the desired dependency.

Parameters:

  • hunt (Hunt)

    to execute.

Returns:

  • (Object)

    an object that satisfies the hunt contract and traits.



75
76
77
# File 'lib/scorpion.rb', line 75

def execute( hunt )
  fail "Not implemented"
end

#fetch(contract, *arguments, **dependencies, &block) ⇒ Object

Hunts for an object that satisfies the requested contract regardless of traits.

See Also:



36
37
38
# File 'lib/scorpion.rb', line 36

def fetch( contract, *arguments, **dependencies, &block )
  fetch_by_traits( contract, nil, *arguments, **dependencies, &block )
end

#fetch_by_traits(contract, traits, *arguments, **dependencies, &block) ⇒ Object

Hunts for an object that satisfies the requested contract and traits.

Parameters:

  • contract (Class, Module, Symbol)

    describing the desired behavior of the dependency.

  • traits (Array<Symbol>)

    required of the dependency

  • dependencies (Hash<Symbol,Object>)

    to inject into the object.

Returns:

  • (Object)

    an object that satisfies the contract and traits.

Raises:



28
29
30
31
# File 'lib/scorpion.rb', line 28

def fetch_by_traits( contract, traits, *arguments, **dependencies, &block )
  hunt = Hunt.new( self, contract, traits, *arguments, **dependencies, &block )
  execute hunt
end

#inject(target) ⇒ target

Inject the #target with all non-lazy dependencies.

Parameters:

Returns:

  • (target)


56
57
58
59
60
61
# File 'lib/scorpion.rb', line 56

def inject( target )
  hunt = Scorpion::Hunt.new self, nil, nil
  hunt.inject target

  target
end

#new(object_class, *arguments, **dependencies, &block) ⇒ Scorpion::Object

Explicitly spawn an instance of #object_class and inject it's dependencies.

Parameters:

  • args (Array<Object>)

    to pass to the constructor.

  • block (#call)

    to pass to the constructor.

Returns:



67
68
69
70
# File 'lib/scorpion.rb', line 67

def new( object_class, *arguments, **dependencies, &block )
  hunt = Hunt.new( self, object_class, nil, *arguments, **dependencies, &block )
  Scorpion::Dependency::ClassDependency.new( object_class ).fetch( hunt )
end

#replicate(&block) ⇒ Scorpion

Creates a new Scorpion copying the current configuration any any currently captured dependency.

Returns:

  • (Scorpion)

    the replicated scorpion.



82
83
84
# File 'lib/scorpion.rb', line 82

def replicate( &block )
  fail "Not implemented"
end

#resetObject

Reset the hunting map and clear all dependencies.



92
93
# File 'lib/scorpion.rb', line 92

def reset
end

#spawn(hunt, object_class, *arguments, **dependencies, &block) ⇒ Scorpion::Object

Creates a new object and feeds it it's dependencies.

Parameters:

  • object_class (Class)

    a class that includes Object.

  • args (Array<Object>)

    to pass to the constructor.

  • block (#call)

    to pass to the constructor.

Returns:



45
46
47
48
49
50
51
# File 'lib/scorpion.rb', line 45

def spawn( hunt, object_class, *arguments, **dependencies, &block )
  if object_class < Scorpion::Object
    object_class.spawn hunt, *arguments, **dependencies, &block
  else
    object_class.new *arguments, &block
  end
end