module Scorpion::Stinger
Utility methods for propagating a Scorpion
to returned objects.
Public Class Methods
new( instance, stinger )
click to toggle source
# File lib/scorpion/stinger.rb, line 11 def initialize( instance, stinger ) @__instance__ = instance @__stinger__ = stinger end
wrap( instance, stinger )
click to toggle source
# File lib/scorpion/stinger.rb, line 6 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 ) # rubocop:disable Style/MethodMissingSuper @__stinger__.sting! @__instance__.__send__( *args, &block ) end end klass.new instance, stinger end
Public Instance Methods
method_missing( *args, &block )
click to toggle source
# File lib/scorpion/stinger.rb, line 22 def method_missing( *args, &block ) # rubocop:disable Style/MethodMissingSuper @__stinger__.sting! @__instance__.__send__( *args, &block ) end
respond_to?( *args )
click to toggle source
# File lib/scorpion/stinger.rb, line 16 def respond_to?( *args ) @__instance__.respond_to?( *args ) end
sting!( object )
click to toggle source
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.
# File lib/scorpion/stinger.rb, line 34 def sting!( object ) return object unless scorpion if object assign_scorpion object assign_scorpion_to_enumerable object end object end
Private Instance Methods
assign_scorpion( object )
click to toggle source
# File lib/scorpion/stinger.rb, line 47 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 # rubocop:disable Metrics/LineLength else object.send :scorpion=, scorpion end end
assign_scorpion_to_enumerable( objects )
click to toggle source
# File lib/scorpion/stinger.rb, line 59 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