Class: Clock
- Inherits:
-
Object
- Object
- Clock
- Defined in:
- lib/clock/mock_clock.rb,
lib/clock/clock.rb,
lib/clock/real_clock.rb
Overview
THREADING NOTE: The mock Clock is not thread-safe, ||= is not atomic. Thus, thread A could modify @@now between thread B’s comparison of @@now to nil and assignment of @@now to Time.now. The #tick method has a similar issue.
This should generally not be a problem, as tests shouldn’t have a reason to modify the current time concurrently in multiple threads.
Class Method Summary collapse
- .now ⇒ Object
- .now=(new) ⇒ Object
- .tick(duration) ⇒ Object
- .today ⇒ Object
- .use_mock_clock? ⇒ Boolean
- .zone ⇒ Object
Class Method Details
.now ⇒ Object
10 11 12 |
# File 'lib/clock/mock_clock.rb', line 10 def self.now @@now ||= Time.now end |
.now=(new) ⇒ Object
14 15 16 |
# File 'lib/clock/mock_clock.rb', line 14 def self.now=(new) @@now = new.to_time end |
.tick(duration) ⇒ Object
18 19 20 |
# File 'lib/clock/mock_clock.rb', line 18 def self.tick(duration) self.now += duration end |
.today ⇒ Object
2 3 4 |
# File 'lib/clock/clock.rb', line 2 def self.today self.now.to_date end |
.use_mock_clock? ⇒ Boolean
6 7 8 9 |
# File 'lib/clock/clock.rb', line 6 def self.use_mock_clock? return USE_MOCK_CLOCK if Object.const_defined?(:USE_MOCK_CLOCK) return ::Rails.env.test? end |
.zone ⇒ Object
22 23 24 25 26 |
# File 'lib/clock/mock_clock.rb', line 22 def self.zone require 'clock/time_zone_proxy' return nil if Time.zone.nil? TimeZoneProxy.new(Time.zone) end |