class RR::Injections::MethodMissingInjection

Constants

BoundObjects

Attributes

subject_class[R]

Public Class Methods

new(subject_class) click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 22
def initialize(subject_class)
  @subject_class = subject_class
  @placeholder_method_defined = false
end

Public Instance Methods

bind() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 27
def bind
  unless class_instance_method_defined(subject_class, original_method_alias_name)
    unless class_instance_method_defined(subject_class, :method_missing)
      @placeholder_method_defined = true
      subject_class.class_eval do
        def method_missing(method_name, *args, &block)
          super
        end
      end
    end
    # Ruby 1.9 will raise a NoMethodError when #method_missing is defined
    # on the subject, but #to_ary isn't. #method_missing will always be
    # defined thanks to BasicObject, but #to_ary may not, so in this case
    # we need to supply our own. Furthermore, Ruby has special logic to
    # handle the return value of #to_ary; if it is nil, then it tells Ruby
    # to ignore #to_ary altogether and use a default rule to arrayify the
    # object in question.
    unless class_instance_method_defined(subject_class, :to_ary)
      subject_class.class_eval do
        def to_ary; nil; end
      end
    end
    subject_class.__send__(:alias_method, original_method_alias_name, :method_missing)
    bind_method
  end
  self
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/rr/injections/method_missing_injection.rb, line 32
def method_missing(method_name, *args, &block)
  super
end
reset() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 55
def reset
  if subject_has_method_defined?(original_method_alias_name)
    memoized_original_method_alias_name = original_method_alias_name
    placeholder_method_defined = @placeholder_method_defined
    subject_class.class_eval do
      remove_method :method_missing
      unless placeholder_method_defined
        alias_method :method_missing, memoized_original_method_alias_name
      end
      remove_method memoized_original_method_alias_name
    end
  end
end
to_ary() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 46
def to_ary; nil; end

Protected Instance Methods

bind_method() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 70
      def bind_method
        id = BoundObjects.size
        BoundObjects[id] = subject_class

        subject_class.class_eval((<<-METHOD), __FILE__, __LINE__ + 1)
          def method_missing(method_name, *args, &block)
            if respond_to_missing?(method_name, true)
              super
            else
              obj = ::RR::Injections::MethodMissingInjection::BoundObjects[#{id}]
              MethodDispatches::MethodMissingDispatch.new(self, obj, method_name, args, block).call
            end
          end
        METHOD
      end
original_method_alias_name() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 86
def original_method_alias_name
  MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name
end