class Mocha::ClassMethod

Constants

PrependedModule

Attributes

method_name[R]
original_method[R]
original_visibility[R]
stubbee[R]

Public Class Methods

new(stubbee, method_name) click to toggle source
# File lib/mocha/class_method.rb, line 10
def initialize(stubbee, method_name)
  @stubbee = stubbee
  @original_method = nil
  @original_visibility = nil
  @method_name = PRE_RUBY_V19 ? method_name.to_s : method_name.to_sym
end

Public Instance Methods

define_new_method() click to toggle source
# File lib/mocha/class_method.rb, line 57
def define_new_method
  stub_method_owner.class_eval(*stub_method_definition)
  return unless original_visibility
  Module.instance_method(original_visibility).bind(stub_method_owner).call(method_name)
end
hide_original_method() click to toggle source
# File lib/mocha/class_method.rb, line 38
def hide_original_method
  return unless method_defined_in_stubbee_or_in_ancestor_chain?
  store_original_method_visibility
  if use_prepended_module_for_stub_method?
    use_prepended_module_for_stub_method
  else
    begin
      store_original_method
    # rubocop:disable Lint/HandleExceptions
    rescue NameError
      # deal with nasties like ActiveRecord::Associations::AssociationProxy
    end
    # rubocop:enable Lint/HandleExceptions
    if stub_method_overwrites_original_method?
      remove_original_method_from_stubbee
    end
  end
end
matches?(other) click to toggle source
# File lib/mocha/class_method.rb, line 83
def matches?(other)
  return false unless other.class == self.class
  (stubbee.object_id == other.stubbee.object_id) && (method_name == other.method_name)
end
method_defined_in_stubbee_or_in_ancestor_chain?()
Alias for: method_visibility
method_visibility() click to toggle source
# File lib/mocha/class_method.rb, line 94
def method_visibility
  (original_method_owner.public_method_defined?(method_name) && :public) ||
    (original_method_owner.protected_method_defined?(method_name) && :protected) ||
    (original_method_owner.private_method_defined?(method_name) && :private)
end
mock() click to toggle source
# File lib/mocha/class_method.rb, line 30
def mock
  stubbee.mocha
end
remove_new_method() click to toggle source
# File lib/mocha/class_method.rb, line 63
def remove_new_method
  stub_method_owner.send(:remove_method, method_name)
end
reset_mocha() click to toggle source
# File lib/mocha/class_method.rb, line 34
def reset_mocha
  stubbee.reset_mocha
end
restore_original_method() click to toggle source
# File lib/mocha/class_method.rb, line 67
def restore_original_method
  return if use_prepended_module_for_stub_method?
  if stub_method_overwrites_original_method?
    if PRE_RUBY_V19
      original_method_in_scope = original_method
      original_method_owner.send(:define_method, method_name) do |*args, &block|
        original_method_in_scope.call(*args, &block)
      end
    else
      original_method_owner.send(:define_method, method_name, original_method)
    end
  end
  return unless original_visibility
  Module.instance_method(original_visibility).bind(stubbee.__metaclass__).call(method_name)
end
stub() click to toggle source
# File lib/mocha/class_method.rb, line 17
def stub
  hide_original_method
  define_new_method
end
to_s() click to toggle source
# File lib/mocha/class_method.rb, line 90
def to_s
  "#{stubbee}.#{method_name}"
end
unstub() click to toggle source
# File lib/mocha/class_method.rb, line 22
def unstub
  remove_new_method
  restore_original_method
  mock.unstub(method_name.to_sym)
  return if mock.any_expectations?
  reset_mocha
end

Private Instance Methods

original_method_owner() click to toggle source
# File lib/mocha/class_method.rb, line 143
def original_method_owner
  stubbee.__metaclass__
end
remove_original_method_from_stubbee() click to toggle source
# File lib/mocha/class_method.rb, line 117
def remove_original_method_from_stubbee
  original_method_owner.send(:remove_method, method_name)
end
store_original_method() click to toggle source
# File lib/mocha/class_method.rb, line 105
def store_original_method
  @original_method = stubbee._method(method_name)
end
store_original_method_visibility() click to toggle source
# File lib/mocha/class_method.rb, line 109
def store_original_method_visibility
  @original_visibility = method_visibility
end
stub_method_definition() click to toggle source
# File lib/mocha/class_method.rb, line 130
    def stub_method_definition
      method_implementation = <<-CODE
      def #{method_name}(*args, &block)
        mocha.method_missing(:#{method_name}, *args, &block)
      end
      CODE
      [method_implementation, __FILE__, __LINE__ - 4]
    end
stub_method_overwrites_original_method?() click to toggle source
# File lib/mocha/class_method.rb, line 113
def stub_method_overwrites_original_method?
  original_method && original_method.owner == original_method_owner
end
stub_method_owner() click to toggle source
# File lib/mocha/class_method.rb, line 139
def stub_method_owner
  @stub_method_owner ||= original_method_owner
end
use_prepended_module_for_stub_method() click to toggle source
# File lib/mocha/class_method.rb, line 125
def use_prepended_module_for_stub_method
  @stub_method_owner = PrependedModule.new
  original_method_owner.__send__ :prepend, @stub_method_owner
end
use_prepended_module_for_stub_method?() click to toggle source
# File lib/mocha/class_method.rb, line 121
def use_prepended_module_for_stub_method?
  RUBY_V2_PLUS
end