Sha256: 5d4cd0c8e72afd8c31de5298443d9c17c71e46cd907b140c98f9ed4ae755f589

Contents?: true

Size: 1.83 KB

Versions: 26

Compression:

Stored size: 1.83 KB

Contents

module Scorpion
  # Utility methods for propagating a Scorpion to returned objects.
  module Stinger
    @wrappers ||= {}

    def self.wrap( instance, stinger )
      return instance unless instance

      klass = @wrappers[instance.class] ||=
        Class.new( instance.class ) do
          def initialize( instance, stinger )
            @__instance__ = instance
            @__stinger__  = stinger
          end

          def respond_to?( *args )
            @__instance__.respond_to?( *args )
          end

          private
            def method_missing( *args, &block )
              @__stinger__.sting! @__instance__.__send__( *args, &block )
            end
        end

      klass.new instance, stinger
    end

    # Sting an object so that it will be injected with the scorpion and use it
    # to resolve all dependencies.
    # @param [#scorpion] object to sting.
    # @return [object] the object that was stung.
    def sting!( object )
      return object unless scorpion

      if object
        assign_scorpion object
        assign_scorpion_to_enumerable object
      end

      object
    end

    private

      def assign_scorpion( object )
        return unless object.respond_to?( :scorpion=, true )

        # Only set scorpion if it hasn't been set yet.
        current_scorpion = object.send :scorpion
        if current_scorpion
          scorpion.logger.warn I18n.translate :mixed_scorpions, scope: [:scorpion,:warnings,:messages] if current_scorpion != scorpion
        else
          object.send :scorpion=, scorpion
        end
      end

      def assign_scorpion_to_enumerable( objects )
        return unless objects.respond_to? :each

        # Don't eager load relations that haven't been loaded yet.
        return if objects.respond_to?( :loaded? ) && ! objects.loaded?

        objects.each{ |v| sting! v }
      end

  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
scorpion-ioc-0.6.2 lib/scorpion/stinger.rb
scorpion-ioc-0.6.1 lib/scorpion/stinger.rb
scorpion-ioc-0.6.0 lib/scorpion/stinger.rb
scorpion-ioc-0.5.23 lib/scorpion/stinger.rb
scorpion-ioc-0.5.22 lib/scorpion/stinger.rb
scorpion-ioc-0.5.21 lib/scorpion/stinger.rb
scorpion-ioc-0.5.20 lib/scorpion/stinger.rb
scorpion-ioc-0.5.19 lib/scorpion/stinger.rb
scorpion-ioc-0.5.18 lib/scorpion/stinger.rb
scorpion-ioc-0.5.17 lib/scorpion/stinger.rb
scorpion-ioc-0.5.16 lib/scorpion/stinger.rb
scorpion-ioc-0.5.15 lib/scorpion/stinger.rb
scorpion-ioc-0.5.14 lib/scorpion/stinger.rb
scorpion-ioc-0.5.13 lib/scorpion/stinger.rb
scorpion-ioc-0.5.12 lib/scorpion/stinger.rb
scorpion-ioc-0.5.11 lib/scorpion/stinger.rb
scorpion-ioc-0.5.10 lib/scorpion/stinger.rb
scorpion-ioc-0.5.9 lib/scorpion/stinger.rb
scorpion-ioc-0.5.8 lib/scorpion/stinger.rb
scorpion-ioc-0.5.7 lib/scorpion/stinger.rb