Sha256: c1fabf44c68c3c003527dc45463434fa223045b3000de8ea5058ddee28f8e13e

Contents?: true

Size: 1.33 KB

Versions: 18

Compression:

Stored size: 1.33 KB

Contents

require 'mocha/class_method'

module Mocha

  class AnyInstanceMethod < ClassMethod
  
    def mock
      stubbee.any_instance.mocha
    end
   
    def hide_original_method
      if method_exists?(method)
        begin
          stubbee.send(:alias_method, hidden_method, method)
        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 method_exists?(hidden_method)
        begin
          stubbee.send(:alias_method, method, hidden_method)
          stubbee.send(:remove_method, hidden_method)
        rescue NameError
          # deal with nasties like ActiveRecord::Associations::AssociationProxy
        end
      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

18 entries across 18 versions & 2 rubygems

Version Path
challah-0.6.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.6.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.5.4 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.5.3 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.5.2 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.5.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.4.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.4.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.5 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.4 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.3 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.2 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.3.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.2.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
challah-0.2.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/any_instance_method.rb
mocha-0.10.3 lib/mocha/any_instance_method.rb
mocha-0.10.2 lib/mocha/any_instance_method.rb