timerizer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/timerizer.rb +62 -0
  2. metadata +3 -3
data/lib/timerizer.rb CHANGED
@@ -259,8 +259,12 @@ class RelativeTime
259
259
  end.reverse.join(', ')
260
260
  end
261
261
  end
262
+
262
263
  # {Time} class monkeywrenched with {RelativeTime} support.
263
264
  class Time
265
+ class TimeIsInThePastException < Exception; end
266
+ class TimeIsInTheFutureException < Exception; end
267
+
264
268
  add = instance_method(:+)
265
269
  define_method(:+) do |time|
266
270
  if(time.class == RelativeTime)
@@ -279,6 +283,52 @@ class Time
279
283
  end
280
284
  end
281
285
 
286
+ # Calculates the time until a given time
287
+ # @param [Time] time The time until now to calculate
288
+ # @return [RelativeTime] The time until the provided time
289
+ # @raise[TimeIsInThePastException] The provided time is in the past
290
+ # @example
291
+ # Time.until(Time.new(2012, 12, 25))
292
+ # => 13 weeks, 2 days, 6 hours, 31 minutes, 39 seconds
293
+ # @see Time#since
294
+ # @see Time#between
295
+ def self.until(time)
296
+ raise TimeIsInThePastException if Time.now > time
297
+
298
+ Time.between(Time.now, time)
299
+ end
300
+
301
+ # Calculates the time since a given time
302
+ # @param [Time] since The time to calculate since now
303
+ # @return [RelativeTime] The time since the provided time
304
+ # @raise[TimeIsInTheFutureException] The provided time is in the future
305
+ # @example
306
+ # Time.since(Time.new(2011, 10, 31))
307
+ # => 46 weeks, 5 days, 18 hours, 26 minutes, 10 seconds
308
+ # @see Time#since
309
+ # @see Time#between
310
+ def self.since(time)
311
+ raise TimeIsInTheFutureException if time > Time.now
312
+
313
+ Time.between(Time.now, time)
314
+ end
315
+
316
+ # Calculate the amount of time between two times.
317
+ # @param [Time] time1 The initial time
318
+ # @param [Time] time2 The final time
319
+ # @return [RelativeTime] Calculated time between time1 and time2
320
+ # @example
321
+ # Time.between(1.minute.ago, 1.hour.ago)
322
+ # => 59.minutes
323
+ # @note The two times are interchangable; which comes first doesn't matter
324
+ # @see Time#until
325
+ # @see Time#since
326
+ def self.between(time1, time2)
327
+ time_between = (time2 - time1).abs
328
+
329
+ RelativeTime.new(time_between.round)
330
+ end
331
+
282
332
  # Convert {Time} to {Date}.
283
333
  # @return [Date] {Time} as {Date}
284
334
  # @example
@@ -317,6 +367,18 @@ class Date
317
367
  def to_date
318
368
  self
319
369
  end
370
+
371
+ # Return tomorrow as {Date}.
372
+ # @see Date#yesterday
373
+ def self.tomorrow
374
+ 1.day.from_now.to_date
375
+ end
376
+
377
+ # Return yesterday as {Date}.
378
+ # @see Date#tomorrow
379
+ def self.yesterday
380
+ 1.day.ago.to_date
381
+ end
320
382
  end
321
383
 
322
384
  # Monkeywrenched {Fixnum} class enabled to return {RelativeTime} objects.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timerizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-17 00:00:00.000000000 Z
12
+ date: 2012-09-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple set of Rails-like time helpers
15
15
  email: kylelacy@me.com
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 1.8.24
41
+ rubygems_version: 1.8.23
42
42
  signing_key:
43
43
  specification_version: 3
44
44
  summary: Rails time helpers... without the Rails