Class: Clock

Inherits:
Object
  • Object
show all
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

Class Method Details

.nowObject



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

.todayObject



2
3
4
# File 'lib/clock/clock.rb', line 2

def self.today
  self.now.to_date
end

.use_mock_clock?Boolean

Returns:

  • (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

.zoneObject



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