Browse files

Tease out Object#acts_like? behaviors

  • Loading branch information...
1 parent 389b081 commit 609c1988d2e274b365c5299cc5933fb6855e4175 @jeremy jeremy committed Mar 28, 2009
View
5 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
View
13 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
View
10 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
View
10 activesupport/lib/active_support/core_ext/object/misc.rb
@@ -77,14 +77,4 @@ def tap
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
View
1 activesupport/lib/active_support/core_ext/time.rb
@@ -4,6 +4,7 @@
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'
View
8 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
View
13 activesupport/lib/active_support/core_ext/time/behavior.rb
@@ -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
View
4 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'

0 comments on commit 609c198

Please sign in to comment.