Class: ActiveSupport::Testing::SimpleStubs

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/testing/time_helpers.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Stub

Instance Method Summary collapse

Constructor Details

#initializeSimpleStubs

Returns a new instance of SimpleStubs.



12
13
14
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 12

def initialize
  @stubs = Concurrent::Map.new { |h, k| h[k] = {} }
end

Instance Method Details

#stub_object(object, method_name, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 16

def stub_object(object, method_name, &block)
  if stub = stubbing(object, method_name)
    unstub_object(stub)
  end

  new_name = "__simple_stub__#{method_name}"

  @stubs[object.object_id][method_name] = Stub.new(object, method_name, new_name)

  object.singleton_class.alias_method new_name, method_name
  object.define_singleton_method(method_name, &block)
end

#stubbed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 42

def stubbed?
  !@stubs.empty?
end

#stubbing(object, method_name) ⇒ Object



38
39
40
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 38

def stubbing(object, method_name)
  @stubs[object.object_id][method_name]
end

#unstub_all!Object



29
30
31
32
33
34
35
36
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 29

def unstub_all!
  @stubs.each_value do |object_stubs|
    object_stubs.each_value do |stub|
      unstub_object(stub)
    end
  end
  @stubs.clear
end