Class: Scorpion::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/scorpion/dependency.rb,
lib/scorpion/dependency/class_dependency.rb,
lib/scorpion/dependency/module_dependency.rb,
lib/scorpion/dependency/builder_dependency.rb,
lib/scorpion/dependency/argument_dependency.rb,
lib/scorpion/dependency/captured_dependency.rb

Overview

Dependency that can be injected into a Object by a Scorpion.

Defined Under Namespace

Classes: ArgumentDependency, BuilderDependency, CapturedDependency, ClassDependency, ModuleDependency

Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract) ⇒ Dependency

Returns a new instance of Dependency.



22
23
24
# File 'lib/scorpion/dependency.rb', line 22

def initialize( contract )
  @contract = contract
end

Instance Attribute Details

#contractClass, ...

Returns contract describing the desired behavior of the dependency.

Returns:

  • (Class, Module, Symbol)

    contract describing the desired behavior of the dependency.



17
18
19
# File 'lib/scorpion/dependency.rb', line 17

def contract
  @contract
end

Class Method Details

.define(contract, options = {}, &builder) ⇒ Dependency

Define dependency based on the desired contract.

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scorpion/dependency.rb', line 79

def define( contract, options = {}, &builder )
  if options.key?( :return )
    Scorpion::Dependency::BuilderDependency.new( contract ) do
      options[:return]
    end
  elsif with = options[ :with ]
    Scorpion::Dependency::BuilderDependency.new( contract, with )
  elsif block_given?
    Scorpion::Dependency::BuilderDependency.new( contract, builder )

  # Allow a Class/Module to define a #create method that will resolve
  # and return an instance of itself. Do not automatically inherit the
  # #create method so only consider it if the owner of the method is the
  # contract itself.
  elsif contract.respond_to?( :create ) && contract.singleton_methods( false ).include?( :create )
    Scorpion::Dependency::BuilderDependency.new( contract ) do |hunt, *args, &block|
      contract.create hunt, *args, &block
    end
  else
    dependency_class( contract ).new( contract, &builder )
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



48
49
50
51
# File 'lib/scorpion/dependency.rb', line 48

def ==( other )
  return unless other
  self.class == other.class && contract == other.contract
end

#fetch(hunt) ⇒ Object

Fetch an instance of the dependency.

Parameters:

  • the (Hunt)

    hunting context.

Returns:

  • (Object)

    the hunted dependency.



34
35
36
# File 'lib/scorpion/dependency.rb', line 34

def fetch( hunt )
  fail "Not Implemented"
end

#hashObject



54
55
56
# File 'lib/scorpion/dependency.rb', line 54

def hash
  self.class.hash ^ contract.hash
end

#inspectObject



58
59
60
61
62
# File 'lib/scorpion/dependency.rb', line 58

def inspect
  result = "<#{ contract.inspect }"
  result << ">"
  result
end

#releaseObject

Release the dependency, freeing up any long held resources.



39
40
# File 'lib/scorpion/dependency.rb', line 39

def release
end

#replicateDependency

Replicate the Dependency.

Returns:

  • (Dependency)

    a replication of the dependency.



44
45
46
# File 'lib/scorpion/dependency.rb', line 44

def replicate
  dup
end

#satisfies?(contract) ⇒ Boolean

Returns if the dependency satisfies the required contract.

Returns:

  • (Boolean)

    if the dependency satisfies the required contract.



27
28
29
# File 'lib/scorpion/dependency.rb', line 27

def satisfies?( contract )
  satisfies_contract?( contract )
end