module Scorpion::Rails::Nest

Handles building a scorpion to handle a single request and populating all the dependencies automatically.

The host class must respond to scorpion, assign_scorpion(scorpion) and free_scorpion.

Public Class Methods

capture( *args, &block ) click to toggle source

Define dependency resolution that isn't resolved until an instance of a scorpion is conceived to handle an idividual request. @param (see DependencyMap#capture )

# File lib/scorpion/rails/nest.rb, line 69
def self.capture( *args, &block )
  instance_hunts << [:capture, args, block]
end
hunt_for( *args, &block ) click to toggle source

Define dependency resolution that isn't resolved until an instance of a scorpion is conceived to handle an idividual request. @param (see DependencyMap#hunt_for )

# File lib/scorpion/rails/nest.rb, line 62
def self.hunt_for( *args, &block )
  instance_hunts << [:hunt_for, args, block]
end
included( base ) click to toggle source

@!endgroup Attributes

Calls superclass method Scorpion::Object::included
# File lib/scorpion/rails/nest.rb, line 35
def self.included( base ) # rubocop:disable Metrics/MethodLength
  # @!attribute [rw]
  # @return [Scorpion::Nest] the singleton nest used by controllers.
  base.class_attribute :nest_instance
  base.class_exec do

    # @!attribute
    # @return [Scorpion::Nest] the nest used to conceive scorpions to
    #   hunt for objects on each request.
    def self.nest
      nest_instance || ( self.nest = Scorpion.instance.build_nest )
    end

    def self.nest=( value )
      nest_instance&.destroy
      self.nest_instance = value
    end

    # Prepare the nest for conceiving scorpions.
    # @see DependencyMap#chart
    def self.scorpion_nest( &block )
      nest.prepare &block
    end

    # Define dependency resolution that isn't resolved until an instance
    # of a scorpion is conceived to handle an idividual request.
    # @param (see DependencyMap#hunt_for )
    def self.hunt_for( *args, &block )
      instance_hunts << [:hunt_for, args, block]
    end

    # Define dependency resolution that isn't resolved until an instance
    # of a scorpion is conceived to handle an idividual request.
    # @param (see DependencyMap#capture )
    def self.capture( *args, &block )
      instance_hunts << [:capture, args, block]
    end

    # Hunting dependencies that cannot be resolved until an instance
    # of the nest class has been created.
    def self.instance_hunts
      @instance_hunts ||= begin
        if superclass.respond_to?( :instance_hunts )
          superclass.instance_hunts.dup
        else
          []
        end
      end
    end
  end


  super
end
instance_hunts() click to toggle source

Hunting dependencies that cannot be resolved until an instance of the nest class has been created.

# File lib/scorpion/rails/nest.rb, line 75
def self.instance_hunts
  @instance_hunts ||= begin
    if superclass.respond_to?( :instance_hunts )
      superclass.instance_hunts.dup
    else
      []
    end
  end
end
nest() click to toggle source

@!attribute @return [Scorpion::Nest] the nest used to conceive scorpions to

hunt for objects on each request.
# File lib/scorpion/rails/nest.rb, line 44
def self.nest
  nest_instance || ( self.nest = Scorpion.instance.build_nest )
end
nest=( value ) click to toggle source
# File lib/scorpion/rails/nest.rb, line 48
def self.nest=( value )
  nest_instance&.destroy
  self.nest_instance = value
end
scorpion_nest( &block ) click to toggle source

Prepare the nest for conceiving scorpions. @see DependencyMap#chart

# File lib/scorpion/rails/nest.rb, line 55
def self.scorpion_nest( &block )
  nest.prepare &block
end

Public Instance Methods

scorpion( scope = nil ) click to toggle source
Calls superclass method
# File lib/scorpion/rails/nest.rb, line 25
def scorpion( scope = nil )
  # Make sure a scorpion is always available. Will be freed on the next
  # call to #with_scorpion
  ensure_scorpion( super ) unless scope
  super
end

Private Instance Methods

append_instance_hunts( scorpion ) click to toggle source
# File lib/scorpion/rails/nest.rb, line 112
def append_instance_hunts( scorpion )
  scorpion.prepare do |hunter|
    self.class.instance_hunts.each do |method, args, block|
      hunter.send method, *args do |*method_args|
        instance_exec *method_args, &block
      end
    end
  end
end
conceive_scorpion() click to toggle source
# File lib/scorpion/rails/nest.rb, line 103
def conceive_scorpion
  @conceived_scorpion = true
  nest.conceive
end
conceived_scorpion?() click to toggle source
# File lib/scorpion/rails/nest.rb, line 108
def conceived_scorpion?
  !!@conceived_scorpion
end
ensure_scorpion( existing ) click to toggle source
# File lib/scorpion/rails/nest.rb, line 122
def ensure_scorpion( existing )
  scorpion = existing
  scorpion = assign_scorpion( conceive_scorpion ) unless existing

  prepare_scorpion( scorpion ) if respond_to?( :prepare_scorpion, true )
  append_instance_hunts( scorpion )
  scorpion
end
nest() click to toggle source

@!attribute @return [Scorpion::Nest] the nest used to conceive scorpions.

# File lib/scorpion/rails/nest.rb, line 20
def nest
  self.class.nest
end
with_scorpion( ) { || ... } click to toggle source

Fetch a scorpion and feed the controller it's dependencies, then yield to perform the action within the context of that scorpion.

# File lib/scorpion/rails/nest.rb, line 94
def with_scorpion( &block )
  ensure_scorpion( scorpion )
  scorpion.inject self

  yield
ensure
  free_scorpion
end