aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-01-15 17:12:32 +0000
committerJamis Buck <jamis@37signals.com>2007-01-15 17:12:32 +0000
commitf28eef9a272fd9280416fd8c0984f616ffe102fe (patch)
tree96acb772243c040f574563fc39a9855773d6139e /activesupport
parent8368e160b4acddf0204a84a9f8d360dcd99fe0ce (diff)
downloadrails-f28eef9a272fd9280416fd8c0984f616ffe102fe.tar.gz
rails-f28eef9a272fd9280416fd8c0984f616ffe102fe.tar.bz2
rails-f28eef9a272fd9280416fd8c0984f616ffe102fe.zip
Add Object#acts_like? and Time#acts_like_time? and Date#acts_like_date? to facilitate duck-typing
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5951 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/date/behavior.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/date_time.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb9
-rw-r--r--activesupport/lib/active_support/core_ext/time.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/time/behavior.rb13
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb28
8 files changed, 74 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 148feff061..f1e40389e9 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add Object#acts_like? and Time#acts_like_time? and Date#acts_like_date? to facilitate duck-typing. [Jamis Buck]
+
* Make 1.months and friends accurate by introducing a Duration class. #6835 [eventualbuddha]
* Document Inflector.ordinalize and merge docs from String inflections. #7023 [smeade]
diff --git a/activesupport/lib/active_support/core_ext/date.rb b/activesupport/lib/active_support/core_ext/date.rb
index effb749c25..9d9051dfbe 100644
--- a/activesupport/lib/active_support/core_ext/date.rb
+++ b/activesupport/lib/active_support/core_ext/date.rb
@@ -1,8 +1,10 @@
require 'date'
+require File.dirname(__FILE__) + '/date/behavior'
require File.dirname(__FILE__) + '/date/calculations'
require File.dirname(__FILE__) + '/date/conversions'
class Date#:nodoc:
+ include ActiveSupport::CoreExtensions::Date::Behavior
include ActiveSupport::CoreExtensions::Date::Calculations
include ActiveSupport::CoreExtensions::Date::Conversions
end
diff --git a/activesupport/lib/active_support/core_ext/date/behavior.rb b/activesupport/lib/active_support/core_ext/date/behavior.rb
new file mode 100644
index 0000000000..011cc17cbf
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/date/behavior.rb
@@ -0,0 +1,13 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Date #:nodoc:
+ module Behavior
+ # Enable more predictable duck-typing on Date-like classes. See
+ # Object#acts_like?.
+ def acts_like_date?
+ true
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/date_time.rb b/activesupport/lib/active_support/core_ext/date_time.rb
new file mode 100644
index 0000000000..67463d2352
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/date_time.rb
@@ -0,0 +1,5 @@
+require "#{File.dirname(__FILE__)}/time/behavior"
+
+class DateTime
+ include ActiveSupport::CoreExtensions::Time::Behavior
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index ea8e3a1d6a..aae336e6f4 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -31,4 +31,13 @@ class Object #:nodoc:
def to_json
ActiveSupport::JSON.encode(self)
end
+
+ # A duck-type assistant method. For example, ActiveSupport extends Date
+ # to define an acts_like_date? method, and extends Time to define
+ # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
+ # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
+ # we want to act like Time simply need to define an acts_like_time? method.
+ def acts_like?(duck)
+ respond_to? :"acts_like_#{duck}?"
+ end
end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/time.rb b/activesupport/lib/active_support/core_ext/time.rb
index 9e3c7a0329..a802c065a7 100644
--- a/activesupport/lib/active_support/core_ext/time.rb
+++ b/activesupport/lib/active_support/core_ext/time.rb
@@ -1,7 +1,9 @@
+require File.dirname(__FILE__) + '/time/behavior'
require File.dirname(__FILE__) + '/time/calculations'
require File.dirname(__FILE__) + '/time/conversions'
class Time#:nodoc:
+ include ActiveSupport::CoreExtensions::Time::Behavior
include ActiveSupport::CoreExtensions::Time::Calculations
include ActiveSupport::CoreExtensions::Time::Conversions
end
diff --git a/activesupport/lib/active_support/core_ext/time/behavior.rb b/activesupport/lib/active_support/core_ext/time/behavior.rb
new file mode 100644
index 0000000000..a5c0baacdf
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/time/behavior.rb
@@ -0,0 +1,13 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Time #:nodoc:
+ module Behavior
+ # Enable more predictable duck-typing on Time-like classes. See
+ # Object#acts_like?.
+ def acts_like_time?
+ true
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 9daf8d0ac4..e48d87a198 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -96,6 +96,34 @@ class ObjectTests < Test::Unit::TestCase
assert object.respond_to?(:baz)
end
+ class DuckTime
+ def acts_like_time?
+ true
+ end
+ end
+
+ def test_duck_typing
+ object = Object.new
+ time = Time.now
+ date = Date.today
+ dt = DateTime.new
+ duck = DuckTime.new
+
+ assert !object.acts_like?(:time)
+ assert !object.acts_like?(:date)
+
+ assert time.acts_like?(:time)
+ assert !time.acts_like?(:date)
+
+ assert !date.acts_like?(:time)
+ assert date.acts_like?(:date)
+
+ assert dt.acts_like?(:time)
+ assert dt.acts_like?(:date)
+
+ assert duck.acts_like?(:time)
+ assert !duck.acts_like?(:date)
+ end
end
class ObjectInstanceVariableTest < Test::Unit::TestCase