timecop 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/History.txt +7 -1
  2. data/VERSION.yml +1 -1
  3. data/lib/timecop/timecop.rb +19 -44
  4. metadata +2 -2
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.1 / 2009-09-30
2
+
3
+ * Maintenance
4
+ * DRY up the Timecop class internals.
5
+
1
6
  === 0.3.0 / 2009-09-20
2
7
 
3
8
  * API
@@ -6,7 +11,8 @@
6
11
 
7
12
  * Maintenance
8
13
  * Fix bug that left Time#mock_time set in some instances
9
- * Upped build dependency to jeweler ~> 1.0.2
14
+ * Upped build dependency to jeweler ~> 1.2.1
15
+ * Don't pollute top-level namespace with classes/constants
10
16
 
11
17
  * Documentation
12
18
  * Clearer examples in the README, better description in the gemspec
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 0
3
+ :patch: 1
4
4
  :major: 0
@@ -62,8 +62,7 @@ class Timecop
62
62
  Time.now
63
63
  end
64
64
 
65
- # Reverts back to system's Time.now, Date.today and DateTime.now (if it exists). If freeze_all or rebase_all
66
- # was never called in the first place, this method will have no effect.
65
+ # Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
67
66
  #
68
67
  # Returns Time.now, which is now the real current time.
69
68
  def self.return
@@ -81,32 +80,26 @@ class Timecop
81
80
  # parse the arguments, build our base time units
82
81
  year, month, day, hour, minute, second = parse_travel_args(*args)
83
82
 
83
+ stack_item = StackItem.new(mock_type, year, month, day, hour, minute, second)
84
84
  # perform our action
85
- if mock_type == :freeze
86
- freeze_all(year, month, day, hour, minute, second)
87
- else
88
- move_all(year, month, day, hour, minute, second)
89
- end
85
+ freeze_or_move(stack_item)
86
+
90
87
  # store this time traveling on our stack...
91
- @_stack << StackItem.new(mock_type, year, month, day, hour, minute, second)
88
+ @_stack << stack_item
92
89
 
93
90
  if block_given?
94
91
  begin
95
92
  yield
96
93
  ensure
97
94
  # pull it off the stack...
98
- stack_item = @_stack.pop
95
+ @_stack.pop
99
96
  if @_stack.size == 0
100
97
  # completely unmock if there's nothing to revert back to
101
98
  unmock!
102
99
  else
103
100
  # or reinstantiate the new the top of the stack (could be a :freeze or a :move)
104
101
  new_top = @_stack.last
105
- if new_top.mock_type == :freeze
106
- freeze_all(new_top.year, new_top.month, new_top.day, new_top.hour, new_top.minute, new_top.second)
107
- else
108
- move_all(new_top.year, new_top.month, new_top.day, new_top.hour, new_top.minute, new_top.second)
109
- end
102
+ freeze_or_move(new_top)
110
103
  end
111
104
  end
112
105
  end
@@ -119,43 +112,25 @@ class Timecop
119
112
 
120
113
  private
121
114
 
122
- # Re-bases Time.now, Date.today and DateTime.now (if it exists) to use the time passed in.
123
- # When using this method directly, it is up to the developer to call unset_all to return us
124
- # to sanity.
125
- #
126
- # * If being consumed in a rails app, Time.zone.local will be used to instantiate the time.
127
- # Otherwise, Time.local will be used.
128
- def freeze_all(year, month, day, hour=0, minute=0, second=0)
129
- if Time.respond_to?(:zone) && !Time.zone.nil?
130
- # ActiveSupport loaded
131
- time = Time.zone.local(year, month, day, hour, minute, second)
132
- else
133
- # ActiveSupport not loaded
134
- time = Time.local(year, month, day, hour, minute, second)
115
+ def freeze_or_move(stack_item) #:nodoc:
116
+ if stack_item.mock_type == :freeze
117
+ Time.freeze_time(time_for_stack_item(stack_item))
118
+ else
119
+ Time.move_time(time_for_stack_item(stack_item))
135
120
  end
136
-
137
- Time.freeze_time(time)
138
121
  end
139
-
140
- # Re-bases Time.now, Date.today and DateTime.now to use the time passed in and to continue moving time
141
- # forward. When using this method directly, it is up to the developer to call return to return us to
142
- # sanity.
143
- #
144
- # * If being consumed in a rails app, Time.zone.local will be used to instantiate the time.
145
- # Otherwise, Time.local will be used.
146
- def move_all(year, month, day, hour=0, minute=0, second=0)
122
+
123
+ def time_for_stack_item(stack_item) #:nodoc:
147
124
  if Time.respond_to?(:zone) && !Time.zone.nil?
148
125
  # ActiveSupport loaded
149
- time = Time.zone.local(year, month, day, hour, minute, second)
126
+ time = Time.zone.local(stack_item.year, stack_item.month, stack_item.day, stack_item.hour, stack_item.minute, stack_item.second)
150
127
  else
151
128
  # ActiveSupport not loaded
152
- time = Time.local(year, month, day, hour, minute, second)
153
- end
154
-
155
- Time.move_time(time)
129
+ time = Time.local(stack_item.year, stack_item.month, stack_item.day, stack_item.hour, stack_item.minute, stack_item.second)
130
+ end
156
131
  end
157
-
158
- def parse_travel_args(*args)
132
+
133
+ def parse_travel_args(*args) #:nodoc:
159
134
  arg = args.shift
160
135
  if arg.is_a?(Time) || (Object.const_defined?(:DateTime) && arg.is_a?(DateTime))
161
136
  year, month, day, hour, minute, second = arg.year, arg.month, arg.day, arg.hour, arg.min, arg.sec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timecop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Trupiano
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-20 00:00:00 -04:00
12
+ date: 2009-09-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15