aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-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
7 files changed, 34 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