clock 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ ## Clock
2
+
3
+ Clock is a simple wrapper for Ruby's Time object, with a few fancy additions for testing. In test mode, Clock
4
+ will automatically load the mock_clock methods, allowing you to set the current time with <code>Clock.now=(time)</code>
5
+ and manually advance time with <code>Clock.tick(seconds)</code>.
6
+
7
+ In any other environment other than test, Clock will behave exactly like Time.
8
+
9
+ ## Requirements
10
+
11
+ To initialize properly, this gem requires Rails 3.0 or above.
@@ -0,0 +1,20 @@
1
+ class Clock
2
+ def self.today
3
+ self.now.to_date
4
+ end
5
+ end
6
+
7
+ module Pivotal
8
+ module Clock
9
+ class Railtie < ::Rails::Railtie
10
+ config.before_configuration do
11
+ Pivotal::Clock.use_mock_clock? ? require("mock_clock") : require('real_clock')
12
+ end
13
+ end
14
+
15
+ def self.use_mock_clock?
16
+ return USE_MOCK_CLOCK if Object.const_defined?(:USE_MOCK_CLOCK)
17
+ return ::Rails.env.test?
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ =begin
2
+ THREADING NOTE: The mock Clock is not thread-safe, ||= is not atomic. Thus, thread A could modify @@now between thread
3
+ B's comparison of @@now to nil and assignment of @@now to Time.now. The #tick method has a similar issue.
4
+
5
+ This should generally not be a problem, as tests shouldn't have a reason to modify the current time concurrently in
6
+ multiple threads.
7
+ =end
8
+
9
+ class Clock
10
+ def self.now
11
+ @@now ||= Time.now
12
+ end
13
+
14
+ def self.now=(new)
15
+ @@now = new.to_time
16
+ end
17
+
18
+ def self.tick(duration)
19
+ self.now += duration
20
+ end
21
+
22
+ def self.zone
23
+ return nil if Time.zone.nil?
24
+ TimeZoneProxy.new(Time.zone)
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ class Clock
2
+ def self.now
3
+ Time.now
4
+ end
5
+
6
+ def self.zone
7
+ Time.zone
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ class TimeZoneProxy
2
+ attr_reader :target
3
+
4
+ def initialize(target)
5
+ raise "Target time zone may not be nil" if target.nil?
6
+ @target = target
7
+ end
8
+
9
+ def now
10
+ Clock.now.in_time_zone(@target)
11
+ end
12
+
13
+ def today
14
+ Clock.now.in_time_zone(@target).to_date
15
+ end
16
+
17
+ def ==(rhs)
18
+ super(rhs) || self.target == rhs || (rhs.respond_to?(:target) && self.target == rhs.target)
19
+ end
20
+
21
+ def method_missing(method, *args)
22
+ target.send(method, *args)
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clock
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 3
10
+ version: 0.1.3
11
+ platform: ruby
12
+ authors:
13
+ - Pivotal Labs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-10 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Works exactly like Ruby's standard Time object, but in test mode adds some nice methods for setting or manually advancing time.
38
+ email: pivotal-opensource@googlegroups.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.markdown
45
+ files:
46
+ - lib/clock.rb
47
+ - lib/mock_clock.rb
48
+ - lib/real_clock.rb
49
+ - lib/time_zone_proxy.rb
50
+ - README.markdown
51
+ has_rdoc: true
52
+ homepage: http://github.com/pivotal/clock
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A wrapper for Ruby's Time object that includes a mock for manipulating time in tests
85
+ test_files: []
86
+