123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Testing::SimpleStubs

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: activesupport/lib/active_support/testing/time_helpers.rb

Overview

Manages stubs for TimeHelpers

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newSimpleStubs

[ GitHub ]

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

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

Instance Attribute Details

#stubbed?Boolean (readonly)

Returns true if any stubs are set, false if there are none

[ GitHub ]

  
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 53

def stubbed?
  !@stubs.empty?
end

Instance Method Details

#stub_object(object, method_name, &block)

Stubs object.method_name with the given block If the method is already stubbed, remove that stub so that removing this stub will restore the original implementation.

Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
target = Time.zone.local(2004, 11, 24, 1, 4, 44)
simple_stubs.stub_object(Time, :now) { at(target.to_i) }
Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
[ GitHub ]

  
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 23

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

  new_name = "__simple_stub__#{method_name}__#{object_id}"

  @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

#stubbing(object, method_name)

Returns the SimpleStubs::Stub for object#method_name (nil if it is not stubbed)

[ GitHub ]

  
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 48

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

#unstub_all!

Remove all object-method stubs held by this instance

[ GitHub ]

  
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 37

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

#unstub_object(stub) (private)

Restores the original object.method described by the SimpleStubs::Stub

[ GitHub ]

  
# File 'activesupport/lib/active_support/testing/time_helpers.rb', line 59

def unstub_object(stub)
  singleton_class = stub.object.singleton_class
  singleton_class.silence_redefinition_of_method stub.method_name
  singleton_class.alias_method stub.method_name, stub.original_method
  singleton_class.undef_method stub.original_method
end