Sha256: 4665f66eb56c92d1e42591b550ffddf09fc875e01ad36c0556868cd71b9cd491

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

require 'mocha/ruby_version'
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
      return unless (@original_visibility = method_visibility(method))
      begin
        if RUBY_V2_PLUS
          @definition_target = PrependedModule.new
          stubbee.__send__ :prepend, @definition_target
        else
          @original_method = stubbee.instance_method(method)
          if @original_method && @original_method.owner == stubbee
            stubbee.send(:remove_method, method)
          end
        end
      # rubocop:disable Lint/HandleExceptions
      rescue NameError
        # deal with nasties like ActiveRecord::Associations::AssociationProxy
      end
      # rubocop:enable Lint/HandleExceptions
    end

    def define_new_method
      definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
        def #{method}(*args, &block)
          self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
        end
      CODE
      return unless @original_visibility
      Module.instance_method(@original_visibility).bind(definition_target).call(method)
    end

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

    def restore_original_method
      return if RUBY_V2_PLUS
      return unless @original_method && @original_method.owner == stubbee
      stubbee.send(:define_method, method, @original_method)
      Module.instance_method(@original_visibility).bind(stubbee).call(method)
    end

    def method_visibility(method)
      (stubbee.public_instance_methods(true).include?(method) && :public) ||
        (stubbee.protected_instance_methods(true).include?(method) && :protected) ||
        (stubbee.private_instance_methods(true).include?(method) && :private)
    end

    private

    def definition_target
      @definition_target ||= stubbee
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/mocha-1.8.0/lib/mocha/any_instance_method.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/mocha-1.8.0/lib/mocha/any_instance_method.rb
mocha-1.8.0 lib/mocha/any_instance_method.rb
mocha-1.7.0 lib/mocha/any_instance_method.rb