Sha256: a0b1d9ce1d057622b992baf1ea55d46e1e353c014ec4e2036294a71ac3d82a67

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

module RR
  module Injections
    class SingletonMethodAddedInjection < Injection
      def initialize(subject)
        @subject = subject
        @placeholder_method_defined = false
      end

      def bind
        unless subject.respond_to?(original_method_alias_name)
          unless subject.respond_to?(:singleton_method_added)
            @placeholder_method_defined = true
            subject_class.class_eval do
              def singleton_method_added(method_name)
                super
              end
            end
          end

          memoized_subject = subject
          memoized_space = space
          memoized_original_method_alias_name = original_method_alias_name
          subject_class.__send__(:alias_method, original_method_alias_name, :singleton_method_added)
          subject_class.__send__(:define_method, :singleton_method_added) do |method_name_arg|
            if memoized_space.double_injection_exists?(memoized_subject, method_name_arg)
              memoized_space.double_injection(memoized_subject, method_name_arg).send(:deferred_bind_method)
            end
            send(memoized_original_method_alias_name, method_name_arg)
          end
        end
        self
      end

      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 :singleton_method_added
            unless placeholder_method_defined
              alias_method :singleton_method_added, memoized_original_method_alias_name
            end
            remove_method memoized_original_method_alias_name
          end
        end
      end

      protected
      def subject_class
        class << subject; self; end
      end

      def original_method_alias_name
        "__rr__original_singleton_method_added"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rr-0.10.10 lib/rr/injections/singleton_method_added_injection.rb
rr-0.10.9 lib/rr/injections/singleton_method_added_injection.rb
rr-0.10.8 lib/rr/injections/singleton_method_added_injection.rb
rr-0.10.7 lib/rr/injections/singleton_method_added_injection.rb
rr-0.10.6 lib/rr/injections/singleton_method_added_injection.rb
rr-0.10.5 lib/rr/injections/singleton_method_added_injection.rb