Module: Scorpion::Rails::Nest

Includes:
Object
Defined in:
lib/scorpion/rails/nest.rb

Overview

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.

Attributes collapse

Class Method Summary collapse

Methods included from Object

infest, #injected_attributes, prepended

Methods included from Method

#scorpion_hunt

Class Method Details

.included(base) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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 Method Details

#scorpion(scope = nil) ⇒ Object



25
26
27
28
29
30
# 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