mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added time unit extensions to Fixnum that'll return the period in seconds, like 2.days + 4.hours
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@365 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
576b162725
commit
38e55bac61
4 changed files with 54 additions and 0 deletions
|
@ -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'
|
||||
|
|
1
activesupport/CHANGELOG
Normal file
1
activesupport/CHANGELOG
Normal file
|
@ -0,0 +1 @@
|
|||
* Added time unit extensions to Fixnum that'll return the period in seconds, like 2.days + 4.hours.
|
36
activesupport/lib/core_ext/fixnum_ext.rb
Normal file
36
activesupport/lib/core_ext/fixnum_ext.rb
Normal file
|
@ -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
|
16
activesupport/test/core_ext/fixnum_ext_test.rb
Normal file
16
activesupport/test/core_ext/fixnum_ext_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue