Sha256: edca6c61b4fa6a1c45facb8db1083f60c670b5e933993aeef7d0f65563baa42d

Contents?: true

Size: 1.49 KB

Versions: 27

Compression:

Stored size: 1.49 KB

Contents

require 'mocha/class_method'

module Mocha

  class AnyInstanceMethod < ClassMethod
  
    def unstub
      remove_new_method
      restore_original_method
      stubbee.any_instance.reset_mocha
    end
    
    def mock
      stubbee.any_instance.mocha
    end
   
    def hide_original_method
      if method_exists?(method)
        begin
          stubbee.class_eval("alias_method :#{hidden_method}, :#{method}", __FILE__, __LINE__)
        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.class_eval("remove_method :#{method}", __FILE__, __LINE__)
    end

    def restore_original_method
      if method_exists?(hidden_method)
        begin
          stubbee.class_eval("alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}", __FILE__, __LINE__)
        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

27 entries across 27 versions & 6 rubygems

Version Path
floehopper-mocha-0.9.3.20081220175348 lib/mocha/any_instance_method.rb
floehopper-mocha-0.9.3.20081230175553 lib/mocha/any_instance_method.rb
floehopper-mocha-0.9.4.20090128164727 lib/mocha/any_instance_method.rb
gsterndale-warrant-0.2.0 test/rails_root/vendor/gems/mocha-0.9.4/lib/mocha/any_instance_method.rb
gsterndale-warrant-0.3.0 test/rails_root/vendor/gems/mocha-0.9.4/lib/mocha/any_instance_method.rb
hashrocket-clearance-0.4.0 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
hashrocket-clearance-0.4.1 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
hashrocket-clearance-0.4.2 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
hashrocket-clearance-0.4.3 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thewoolleyman-mocha-0.9.3.20081219131457 lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.2 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.3 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.4 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.5 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.6 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.7 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.8 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.2.9 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.3.0 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb
thoughtbot-clearance-0.3.1 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/any_instance_method.rb