From 14ed815b1c0098f1f7132d0a5a7e22088849c30e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 10 Jan 2005 22:49:45 +0000 Subject: Added Fixnum#ago/until, Fixnum#since/from_now #450 [bitsweat] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@371 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ activesupport/lib/core_ext/fixnum_ext.rb | 18 +++++++++++++++++- activesupport/test/core_ext/fixnum_ext_test.rb | 15 +++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 391447140d..6ac105bde1 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,3 +1,5 @@ +* Added Fixnum#ago/until, Fixnum#since/from_now #450 [bitsweat] + * Added that Inflector now accepts Symbols and Classes by calling .to_s on the word supplied * Added time unit extensions to Fixnum that'll return the period in seconds, like 2.days + 4.hours. \ No newline at end of file diff --git a/activesupport/lib/core_ext/fixnum_ext.rb b/activesupport/lib/core_ext/fixnum_ext.rb index c7b875c021..14944df3bb 100644 --- a/activesupport/lib/core_ext/fixnum_ext.rb +++ b/activesupport/lib/core_ext/fixnum_ext.rb @@ -33,4 +33,20 @@ class Fixnum self * 365.days end alias :year :years -end \ No newline at end of file + + # Reads best without arguments: 10.minutes.ago + def ago(time = Time.now) + time - self + end + + # Reads best with argument: 10.minutes.until(time) + alias :until :ago + + # Reads best with argument: 10.minutes.since(time) + def since(time = Time.now) + time + self + end + + # Reads best without arguments: 10.minutes.from_now + alias :from_now :since +end diff --git a/activesupport/test/core_ext/fixnum_ext_test.rb b/activesupport/test/core_ext/fixnum_ext_test.rb index 4014ad07d0..12ef69596d 100644 --- a/activesupport/test/core_ext/fixnum_ext_test.rb +++ b/activesupport/test/core_ext/fixnum_ext_test.rb @@ -2,15 +2,22 @@ require 'test/unit' require File.dirname(__FILE__) + '/../../lib/core_ext/fixnum_ext' class FixnumExtTest < Test::Unit::TestCase - def test_time_units - expected = { + def setup + @now = Time.now + @seconds = { 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 } + end - expected.each { |actual, expected| assert_equal expected, actual } + def test_time_units + @seconds.each do |actual, expected| + assert_equal expected, actual + assert_equal expected.since(@now), @now + actual + assert_equal expected.until(@now), @now - actual + end end -end \ No newline at end of file +end -- cgit v1.2.3