Sha256: 626f1a2f1c475bf72007fcb100be4317dc9457b40b8ed1d30e6a42b82d6539b6

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'scorpion/nest'

module Scorpion
  module Rails

    # Adds a scorpion nest to support injection into rails controllers.
    module Controller

      ENV_KEY = 'scorpion.instance'.freeze

      def scorpion
        env[ENV_KEY]
      end

      def self.included( base )
        # Setup dependency injection
        base.send :include, Scorpion::Rails::Nest
        base.around_filter :with_scorpion
        super
      end

      private

        # Fetch a scorpion and feed the controller it's dependencies
        def prepare_scorpion( scorpion )
          scorpion.prepare do |hunter|
            # Allow dependencies to access the controller
            hunter.hunt_for AbstractController::Base, return: self

            # Allow dependencies to access the current request/response
            hunter.hunt_for ActionDispatch::Request do |hunt|
              hunt.fetch( AbstractController::Base ).request
            end
            hunter.hunt_for ActionDispatch::Response do |hunt|
              hunt.fetch( AbstractController::Base ).response
            end
          end
        end

        def assign_scorpion( scorpion )
          env[ENV_KEY] = scorpion
        end

        def free_scorpion
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scorpion-ioc-0.4.0 lib/scorpion/rails/controller.rb