aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-03-28 23:43:03 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-03-28 23:43:26 -0700
commit609c1988d2e274b365c5299cc5933fb6855e4175 (patch)
tree83c132d1cca1f3eb632c06d007c76399f9bec5c3
parent389b081e374e0c6f2c124837439628684cd4f704 (diff)
downloadrails-609c1988d2e274b365c5299cc5933fb6855e4175.tar.gz
rails-609c1988d2e274b365c5299cc5933fb6855e4175.tar.bz2
rails-609c1988d2e274b365c5299cc5933fb6855e4175.zip
Tease out Object#acts_like? behaviors
-rw-r--r--activesupport/lib/active_support/core_ext/date/acts_like.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/acts_like.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/object/acts_like.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/time.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/time/acts_like.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/time/behavior.rb13
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb4
8 files changed, 38 insertions, 26 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/acts_like.rb b/activesupport/lib/active_support/core_ext/date/acts_like.rb
index ea6cb38973..cd90cee236 100644
--- a/activesupport/lib/active_support/core_ext/date/acts_like.rb
+++ b/activesupport/lib/active_support/core_ext/date/acts_like.rb
@@ -1,8 +1,7 @@
-require 'date'
+require 'active_support/core_ext/object/acts_like'
class Date
- # Enable more predictable duck-typing on Date-like classes. See
- # Object#acts_like?.
+ # Duck-types as a Date-like class. See Object#acts_like?.
def acts_like_date?
true
end
diff --git a/activesupport/lib/active_support/core_ext/date_time/acts_like.rb b/activesupport/lib/active_support/core_ext/date_time/acts_like.rb
new file mode 100644
index 0000000000..c79745c5aa
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/date_time/acts_like.rb
@@ -0,0 +1,13 @@
+require 'active_support/core_ext/object/acts_like'
+
+class DateTime
+ # Duck-types as a Date-like class. See Object#acts_like?.
+ def acts_like_date?
+ true
+ end
+
+ # Duck-types as a Time-like class. See Object#acts_like?.
+ def acts_like_time?
+ true
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/object/acts_like.rb b/activesupport/lib/active_support/core_ext/object/acts_like.rb
new file mode 100644
index 0000000000..fcc8e50f06
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/object/acts_like.rb
@@ -0,0 +1,10 @@
+class Object
+ # A duck-type assistant method. For example, Active Support 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
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index 4acdfa3d6c..fb1bcdb98f 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -77,14 +77,4 @@ class Object
def with_options(options)
yield ActiveSupport::OptionMerger.new(self, options)
end
-
- # A duck-type assistant method. For example, Active Support 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
diff --git a/activesupport/lib/active_support/core_ext/time.rb b/activesupport/lib/active_support/core_ext/time.rb
index 36022c2562..d56bce8305 100644
--- a/activesupport/lib/active_support/core_ext/time.rb
+++ b/activesupport/lib/active_support/core_ext/time.rb
@@ -4,6 +4,7 @@ require 'time'
require 'active_support/core_ext/time/publicize_conversion_methods'
require 'active_support/core_ext/time/marshal_with_utc_flag'
+require 'active_support/core_ext/time/acts_like'
require 'active_support/core_ext/time/calculations'
require 'active_support/core_ext/time/zones'
diff --git a/activesupport/lib/active_support/core_ext/time/acts_like.rb b/activesupport/lib/active_support/core_ext/time/acts_like.rb
new file mode 100644
index 0000000000..3f853b7893
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/time/acts_like.rb
@@ -0,0 +1,8 @@
+require 'active_support/core_ext/object/acts_like'
+
+class Time
+ # Duck-types as a Time-like class. See Object#acts_like?.
+ def acts_like_time?
+ true
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/time/behavior.rb b/activesupport/lib/active_support/core_ext/time/behavior.rb
deleted file mode 100644
index a5c0baacdf..0000000000
--- a/activesupport/lib/active_support/core_ext/time/behavior.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-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/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index d329804617..531fdb9d31 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -1,7 +1,11 @@
require 'active_support/core_ext/time/publicize_conversion_methods'
+require 'active_support/core_ext/time/acts_like'
require 'active_support/core_ext/time/calculations'
require 'active_support/core_ext/time/zones'
+require 'active_support/core_ext/date/acts_like'
+
+require 'active_support/core_ext/date_time/acts_like'
require 'active_support/core_ext/date_time/calculations'
require 'active_support/core_ext/date_time/zones'