From 609c1988d2e274b365c5299cc5933fb6855e4175 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 28 Mar 2009 23:43:03 -0700 Subject: Tease out Object#acts_like? behaviors --- activesupport/lib/active_support/core_ext/date/acts_like.rb | 5 ++--- .../lib/active_support/core_ext/date_time/acts_like.rb | 13 +++++++++++++ .../lib/active_support/core_ext/object/acts_like.rb | 10 ++++++++++ activesupport/lib/active_support/core_ext/object/misc.rb | 10 ---------- activesupport/lib/active_support/core_ext/time.rb | 1 + activesupport/lib/active_support/core_ext/time/acts_like.rb | 8 ++++++++ activesupport/lib/active_support/core_ext/time/behavior.rb | 13 ------------- activesupport/lib/active_support/values/time_zone.rb | 4 ++++ 8 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/date_time/acts_like.rb create mode 100644 activesupport/lib/active_support/core_ext/object/acts_like.rb create mode 100644 activesupport/lib/active_support/core_ext/time/acts_like.rb delete mode 100644 activesupport/lib/active_support/core_ext/time/behavior.rb 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' -- cgit v1.2.3