class AncientMock::Stubber

Public Class Methods

for_object(obj) click to toggle source
# File lib/ancient_mock.rb, line 28
def self.for_object(obj)
  stubbers[obj.__id__] ||= Stubber.new(obj)
end
new(obj) click to toggle source
# File lib/ancient_mock.rb, line 37
def initialize(obj)
  @obj = obj
  @definitions = []
  @preserved_methods = []
end
reset() click to toggle source
# File lib/ancient_mock.rb, line 32
def self.reset
  stubbers.each_value(&:reset)
  stubbers.clear
end
stubbers() click to toggle source
# File lib/ancient_mock.rb, line 24
def self.stubbers
  @stubbers ||= { }
end

Public Instance Methods

reset() click to toggle source
# File lib/ancient_mock.rb, line 61
def reset
  @definitions.each do |definition|
    @obj.singleton_class.class_eval do
      remove_method(definition.message) if method_defined?(definition.message)
    end
  end

  @preserved_methods.reverse.each do |method|
    @obj.define_singleton_method(method.name, method)
  end
end
stub(definition) click to toggle source
# File lib/ancient_mock.rb, line 43
def stub(definition)
  @definitions << definition

  # preserve the method if already exists
  if @obj.singleton_class.method_defined?(definition.message)
    @preserved_methods <<
      @obj.singleton_class.instance_method(definition.message)
  end

  definitions = @definitions
  @obj.define_singleton_method(definition.message) do |*arguments|
    definitions.
      reverse.
      find { |d| d.matches?(definition.message, *arguments) }.
      call
  end
end