Sha256: 69a0d167614dd7f00f9b7399fb7935a8ff2e02d45e55563a3b7b1a9267bac3c9

Contents?: true

Size: 1.69 KB

Versions: 39

Compression:

Stored size: 1.69 KB

Contents

require 'mocha/class_method'

module Mocha

  class AnyInstanceMethod < ClassMethod

    def mock
      stubbee.any_instance.mocha
    end

    def reset_mocha
      stubbee.any_instance.reset_mocha
    end

    def hide_original_method
      if method_exists?(method)
        begin
          @original_method = stubbee.instance_method(method)
          if @original_method && @original_method.owner == stubbee
            @original_visibility = :public
            if stubbee.protected_instance_methods.include?(method)
              @original_visibility = :protected
            elsif stubbee.private_instance_methods.include?(method)
              @original_visibility = :private
            end
            stubbee.send(:remove_method, method)
          end
        rescue NameError
          # deal with nasties like ActiveRecord::Associations::AssociationProxy
        end
      end
    end

    def define_new_method
      stubbee.class_eval(%{
        def #{method}(*args, &block)
          self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
        end
      }, __FILE__, __LINE__)
    end

    def remove_new_method
      stubbee.send(:remove_method, method)
    end

    def restore_original_method
      if @original_method && @original_method.owner == stubbee
        stubbee.send(:define_method, method, @original_method)
        stubbee.send(@original_visibility, method)
      end
    end

    def method_exists?(method)
      return true if stubbee.public_instance_methods(false).include?(method)
      return true if stubbee.protected_instance_methods(false).include?(method)
      return true if stubbee.private_instance_methods(false).include?(method)
      return false
    end

  end

end

Version data entries

39 entries across 31 versions & 3 rubygems

Version Path
mocha-0.12.10 lib/mocha/any_instance_method.rb
mocha-0.12.9 lib/mocha/any_instance_method.rb
mocha-0.13.2 lib/mocha/any_instance_method.rb
mocha-0.12.8 lib/mocha/any_instance_method.rb
mocha-0.13.1 lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.13.0/lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/mocha-0.12.2/lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/mocha-0.13.0/lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.12.2/lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/mocha-0.12.3/lib/mocha/any_instance_method.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.12.3/lib/mocha/any_instance_method.rb
challah-0.8.3 vendor/bundle/gems/mocha-0.12.3/lib/mocha/any_instance_method.rb
challah-0.8.3 vendor/bundle/gems/mocha-0.13.0/lib/mocha/any_instance_method.rb
mocha-0.13.0 lib/mocha/any_instance_method.rb
mocha-0.12.7 lib/mocha/any_instance_method.rb
mocha-0.12.6 lib/mocha/any_instance_method.rb
mocha-0.12.5 lib/mocha/any_instance_method.rb
challah-0.8.1 vendor/bundle/gems/mocha-0.12.3/lib/mocha/any_instance_method.rb
mocha-0.12.4 lib/mocha/any_instance_method.rb
challah-rolls-0.1.0 vendor/bundle/gems/mocha-0.12.3/lib/mocha/any_instance_method.rb