Permalink
Browse files

Added time unit extensions to Fixnum that'll return the period in sec…

…onds, like 2.days + 4.hours

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@365 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 576b162 commit 38e55bac6197c937c2f1ef356ff3be234758a7c7 @dhh dhh committed Jan 10, 2005
@@ -24,6 +24,7 @@
$:.unshift(File.dirname(__FILE__))
+require 'action_controller/support/core_ext'
require 'active_record/support/clean_logger'
require 'active_record/support/misc'
require 'active_record/support/dependencies'
View
@@ -0,0 +1 @@
+* Added time unit extensions to Fixnum that'll return the period in seconds, like 2.days + 4.hours.
@@ -0,0 +1,36 @@
+class Fixnum
+ def minutes
+ self * 60
+ end
+ alias :minute :minutes
+
+ def hours
+ self * 60.minutes
+ end
+ alias :hour :hours
+
+ def days
+ self * 24.hours
+ end
+ alias :day :days
+
+ def weeks
+ self * 7.days
+ end
+ alias :week :weeks
+
+ def fortnights
+ self * 2.weeks
+ end
+ alias :fortnight :fortnights
+
+ def months
+ self * 30.days
+ end
+ alias :month :months
+
+ def years
+ self * 365.days
+ end
+ alias :year :years
+end
@@ -0,0 +1,16 @@
+require 'test/unit'
+require File.dirname(__FILE__) + '/../../lib/core_ext/fixnum_ext'
+
+class FixnumExtTest < Test::Unit::TestCase
+ def test_time_units
+ expected = {
+ 1.minute => 60,
+ 10.minutes => 600,
+ 1.hour + 15.minutes => 4500,
+ 2.days + 4.hours + 30.minutes => 189000,
+ 5.years + 1.month + 1.fortnight => 161481600
+ }
+
+ expected.each { |actual, expected| assert_equal expected, actual }
+ end
+end

0 comments on commit 38e55ba

Please sign in to comment.