class Mocha::ClassMethod
Constants
- PrependedModule
Attributes
method[R]
stubbee[R]
Public Class Methods
new(stubbee, method)
click to toggle source
# File lib/mocha/class_method.rb, line 10 def initialize(stubbee, method) @stubbee = stubbee @original_method = nil @original_visibility = nil @method = PRE_RUBY_V19 ? method.to_s : method.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 definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1) def #{method}(*args, &block) mocha.method_missing(:#{method}, *args, &block) end CODE return unless @original_visibility Module.instance_method(@original_visibility).bind(definition_target).call(method) end
hide_original_method()
click to toggle source
# File lib/mocha/class_method.rb, line 38 def hide_original_method return unless (@original_visibility = method_visibility(method)) begin if RUBY_V2_PLUS @definition_target = PrependedModule.new stubbee.__metaclass__.__send__ :prepend, @definition_target else @original_method = stubbee._method(method) if @original_method && @original_method.owner == stubbee.__metaclass__ stubbee.__metaclass__.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
matches?(other)
click to toggle source
# File lib/mocha/class_method.rb, line 87 def matches?(other) return false unless other.class == self.class (stubbee.object_id == other.stubbee.object_id) && (method == other.method) end
method_visibility(method)
click to toggle source
# File lib/mocha/class_method.rb, line 98 def method_visibility(method) symbol = method.to_sym metaclass = stubbee.__metaclass__ (metaclass.public_method_defined?(symbol) && :public) || (metaclass.protected_method_defined?(symbol) && :protected) || (metaclass.private_method_defined?(symbol) && :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 67 def remove_new_method definition_target.send(:remove_method, method) 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 71 def restore_original_method return if RUBY_V2_PLUS if @original_method && @original_method.owner == stubbee.__metaclass__ if PRE_RUBY_V19 original_method = @original_method stubbee.__metaclass__.send(:define_method, method) do |*args, &block| original_method.call(*args, &block) end else stubbee.__metaclass__.send(:define_method, method, @original_method) end end return unless @original_visibility Module.instance_method(@original_visibility).bind(stubbee.__metaclass__).call(method) 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 94 def to_s "#{stubbee}.#{method}" 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.to_sym) return if mock.any_expectations? reset_mocha end
Private Instance Methods
definition_target()
click to toggle source
# File lib/mocha/class_method.rb, line 109 def definition_target @definition_target ||= stubbee.__metaclass__ end