class Scorpion::Dependency

Dependency that can be injected into a {Scorpion::Object} by a {Scorpion}.

Attributes

contract[R]

@!attribute @return [Class,Module,Symbol] contract describing the desired behavior of the dependency.

Public Class Methods

new( contract ) click to toggle source

@!endgroup Attributes

# File lib/scorpion/dependency.rb, line 22
def initialize( contract )
  @contract = contract
end

Private Class Methods

define( contract, options = {}, &builder ) click to toggle source

Define dependency based on the desired contract. @return [Dependency] the defined dependency.

# 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
dependency_class( contract, &builder ) click to toggle source
# File lib/scorpion/dependency.rb, line 104
def dependency_class( contract, &builder )
  return Scorpion::Dependency::ClassDependency   if contract.is_a? Class
  return Scorpion::Dependency::ModuleDependency  if contract.is_a? Module

  raise Scorpion::BuilderRequiredError
end

Public Instance Methods

==( other ) click to toggle source
# File lib/scorpion/dependency.rb, line 48
def ==( other )
  return unless other
  self.class == other.class && contract == other.contract
end
Also aliased as: eql?
eql?( other )
Alias for: ==
fetch( hunt ) click to toggle source

Fetch an instance of the dependency. @param [Hunt] the hunting context. @return [Object] the hunted dependency.

# File lib/scorpion/dependency.rb, line 34
def fetch( hunt )
  fail "Not Implemented"
end
hash() click to toggle source
# File lib/scorpion/dependency.rb, line 54
def hash
  self.class.hash ^ contract.hash
end
inspect() click to toggle source
# File lib/scorpion/dependency.rb, line 58
def inspect
  result = "<#{ contract.inspect }"
  result << ">"
  result
end
release() click to toggle source

Release the dependency, freeing up any long held resources.

# File lib/scorpion/dependency.rb, line 39
def release
end
replicate() click to toggle source

Replicate the Dependency. @return [Dependency] a replication of the dependency.

# File lib/scorpion/dependency.rb, line 44
def replicate
  dup
end
satisfies?( contract ) click to toggle source

@return [Boolean] if the dependency satisfies the required contract.

# File lib/scorpion/dependency.rb, line 27
def satisfies?( contract )
  satisfies_contract?( contract )
end

Private Instance Methods

satisfies_contract?( contract ) click to toggle source

@return [Boolean] true if the pray satisfies the given contract.

# File lib/scorpion/dependency.rb, line 67
def satisfies_contract?( contract )
  if self.contract.is_a?(Symbol) || contract.is_a?(Symbol)
    self.contract == contract
  else
    self.contract <= contract
  end
end