module Scorpion::Object

Identifies objects that are injected by {Scorpion scorpions} that inject {Scorpion#hunt hunted} dependencies.

Public Class Methods

included( base ) click to toggle source
Calls superclass method
# File lib/scorpion/object.rb, line 62
def self.included( base )
  infest( base )
  super
end
infest( base ) click to toggle source

Infest the object with a scoprion and prepare it to be fed.

# File lib/scorpion/object.rb, line 36
def self.infest( base )
  base.extend Scorpion::Object::ClassMethods
  if base.is_a? Class
    base.class_exec do

      # Create a new instance of this class with all non-lazy dependencies
      # satisfied.
      # @param [Hunt] hunt that this instance will be used to satisfy.
      def self.spawn( hunt, *args, &block )
        object = new( *args, &block )
        object.send :scorpion=, hunt.scorpion

        # Go hunt for dependencies that are not lazy and initialize the
        # references.
        hunt.inject object
        object
      end

    end

    # base.subclasses.each do |sub|
    #   infest( sub ) unless sub < Scorpion::Object
    # end
  end
end
prepended( base ) click to toggle source
Calls superclass method
# File lib/scorpion/object.rb, line 67
def self.prepended( base )
  infest( base )
  super
end
spawn( hunt, *args, &block ) click to toggle source

Create a new instance of this class with all non-lazy dependencies satisfied. @param [Hunt] hunt that this instance will be used to satisfy.

# File lib/scorpion/object.rb, line 44
def self.spawn( hunt, *args, &block )
  object = new( *args, &block )
  object.send :scorpion=, hunt.scorpion

  # Go hunt for dependencies that are not lazy and initialize the
  # references.
  hunt.inject object
  object
end

Public Instance Methods

inject_dependency( attribute, dependency ) click to toggle source

Injects one of the {#injected_attributes} into the object. @param [Scorpion::Attribute] attribute to be fed. @param [Object] dependency the value of the attribute @visibility private

This method is used by the {#scorpion} to feed the object. Do not call it directly.

# File lib/scorpion/object.rb, line 31
def inject_dependency( attribute, dependency )
  send "#{ attribute.name }=", dependency
end
injected_attributes() click to toggle source

@!attribute @return [Scorpion::AttributeSet] the set of injected attributes and their

settings.
# File lib/scorpion/object.rb, line 17
def injected_attributes
  self.class.injected_attributes
end

Private Instance Methods

inject_from( dependencies, overwrite = false ) click to toggle source

Feed dependencies from a hash into their associated attributes. @param [Hash] dependencies hash describing attributes to inject. @param [Boolean] overwrite existing attributes with values in in the hash.

# File lib/scorpion/object.rb, line 84
def inject_from( dependencies, overwrite = false )
  injected_attributes.each do |attr|
    next unless dependencies.key? attr.name

    if overwrite || !send( "#{ attr.name }?" )
      send( "#{ attr.name }=", dependencies[ attr.name ] )
    end
  end

  dependencies
end
inject_from!( dependencies, overwrite = false ) click to toggle source

Injects dependenices from the hash and removes them from the hash. @see inject_from

# File lib/scorpion/object.rb, line 98
def inject_from!( dependencies, overwrite = false )
  injected_attributes.each do |attr|
    next unless dependencies.key? attr.name
    val = dependencies.delete( attr.name )

    if overwrite || !send( "#{ attr.name }?" )
      send( "#{ attr.name }=", val )
    end
  end

  dependencies
end
on_injected() click to toggle source

Called after the object has been initialized and fed all its required dependencies. It should be used in place of initialize when the constructor needs access to injected attributes.

# File lib/scorpion/object.rb, line 78
def on_injected
end