diff options
Diffstat (limited to 'activesupport/lib')
5 files changed, 32 insertions, 19 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index aa9caf1774..c0175a5f28 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -3,17 +3,22 @@ module ActiveSupport #:nodoc: module DateTime #:nodoc: # Converting datetimes to formatted strings, dates, and times. module Conversions - def self.included(base) #:nodoc: + def self.append_features(base) #:nodoc: base.class_eval do - alias_method :to_default_s, :to_s if instance_methods.include?(:to_s) - alias_method :to_s, :to_formatted_s alias_method :default_inspect, :inspect - alias_method :inspect, :readable_inspect + alias_method :to_default_s, :to_s unless (instance_methods(false) & [:to_s, 'to_s']).empty? # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows # DateTimes outside the range of what can be created with Time. remove_method :to_time if instance_methods.include?(:to_time) end + + super + + base.class_eval do + alias_method :to_s, :to_formatted_s + alias_method :inspect, :readable_inspect + end end # Convert to a formatted string. See Time::DATE_FORMATS for predefined formats. diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 1b96eb166a..8f111e29fc 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -1,4 +1,7 @@ module Enumerable + # Ruby 1.8.7 introduces group_by, but the result isn't ordered. Override it. + remove_method(:group_by) if [].respond_to?(:group_by) && RUBY_VERSION < '1.9' + # Collect an enumerable into sets, grouped by the result of a block. Useful, # for example, for grouping records by date. # @@ -19,7 +22,7 @@ module Enumerable (grouped[yield(element)] ||= []) << element grouped end - end if RUBY_VERSION < '1.9' + end unless [].respond_to?(:group_by) # Calculates a sum from the elements. Examples: # diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index a83474f278..25386af70a 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -2,7 +2,7 @@ require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/string/conversions' require 'active_support/core_ext/string/access' require 'active_support/core_ext/string/starts_ends_with' -require 'active_support/core_ext/string/iterators' unless 'test'.respond_to?(:each_char) +require 'active_support/core_ext/string/iterators' require 'active_support/core_ext/string/unicode' require 'active_support/core_ext/string/xchar' require 'active_support/core_ext/string/filters' @@ -12,14 +12,7 @@ class String #:nodoc: include ActiveSupport::CoreExtensions::String::Conversions include ActiveSupport::CoreExtensions::String::Filters include ActiveSupport::CoreExtensions::String::Inflections - if RUBY_VERSION < '1.9' - include ActiveSupport::CoreExtensions::String::StartsEndsWith - else - alias starts_with? start_with? - alias ends_with? end_with? - end - if defined? ActiveSupport::CoreExtensions::String::Iterators - include ActiveSupport::CoreExtensions::String::Iterators - end + include ActiveSupport::CoreExtensions::String::StartsEndsWith + include ActiveSupport::CoreExtensions::String::Iterators include ActiveSupport::CoreExtensions::String::Unicode end diff --git a/activesupport/lib/active_support/core_ext/string/iterators.rb b/activesupport/lib/active_support/core_ext/string/iterators.rb index 73114d9d5f..66a08a5cd0 100644 --- a/activesupport/lib/active_support/core_ext/string/iterators.rb +++ b/activesupport/lib/active_support/core_ext/string/iterators.rb @@ -5,6 +5,10 @@ module ActiveSupport #:nodoc: module String #:nodoc: # Custom string iterators module Iterators + def self.append_features(base) + super unless '1.9'.respond_to?(:each_char) + end + # Yields a single-character string for each character in the string. # When $KCODE = 'UTF8', multi-byte characters are yielded appropriately. def each_char diff --git a/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb b/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb index 3960669798..09f9a188b5 100644 --- a/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb +++ b/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb @@ -3,10 +3,18 @@ module ActiveSupport #:nodoc: module String #:nodoc: # Additional string tests. module StartsEndsWith - def self.included(base) - base.class_eval do - alias_method :start_with?, :starts_with? - alias_method :end_with?, :ends_with? + def self.append_features(base) + if '1.8.7 and up'.respond_to?(:start_with?) + base.class_eval do + alias_method :starts_with?, :start_with? + alias_method :ends_with?, :end_with? + end + else + super + base.class_eval do + alias_method :start_with?, :starts_with? + alias_method :end_with?, :ends_with? + end end end |