From cf04e621270bb2e5e9e7971d2c59e73d6797482d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Apr 2008 23:30:01 -0500 Subject: Tidy up ActiveSupport::Callbacks::CallbackChain instance API. --- activesupport/lib/active_support/callbacks.rb | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 329cc2fdc7..282b6cbcbc 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -96,17 +96,26 @@ module ActiveSupport end end - def find_callback(callback, &block) + def |(chain) + if chain.is_a?(Callback) + if found_callback = find(chain) + index = index(found_callback) + self[index] = chain + else + self << chain + end + else + chain.each { |callback| self | callback } + end + self + end + + def find(callback, &block) select { |c| c == callback && (!block_given? || yield(c)) }.first end - def replace_or_append_callback(callback) - if found_callback = find_callback(callback) - index = index(found_callback) - self[index] = callback - else - self << callback - end + def delete(callback) + super(find(callback)) end private -- cgit v1.2.3 From a4c15303cbc96fffd66c68abdeebe73d8883e5ab Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 18 Apr 2008 14:44:55 -0500 Subject: Slight optimization to CallbackChain#union and delete. --- activesupport/lib/active_support/callbacks.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 282b6cbcbc..5c51237049 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -97,15 +97,14 @@ module ActiveSupport end def |(chain) - if chain.is_a?(Callback) - if found_callback = find(chain) - index = index(found_callback) + if chain.is_a?(CallbackChain) + chain.each { |callback| self | callback } + elsif chain.is_a?(Callback) + if index = index(chain) self[index] = chain else self << chain end - else - chain.each { |callback| self | callback } end self end @@ -115,7 +114,7 @@ module ActiveSupport end def delete(callback) - super(find(callback)) + super(callback.is_a?(Callback) ? callback : find(callback)) end private -- cgit v1.2.3 From db11ef9546b1ad16cf539395b75450c1c8e9714c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 18 Apr 2008 14:50:10 -0700 Subject: Override Ruby 1.8.7's incompatible Symbol#to_proc. --- activesupport/lib/active_support/core_ext/symbol.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/symbol.rb b/activesupport/lib/active_support/core_ext/symbol.rb index 54f541ad9a..d41d47e70e 100644 --- a/activesupport/lib/active_support/core_ext/symbol.rb +++ b/activesupport/lib/active_support/core_ext/symbol.rb @@ -1,4 +1,11 @@ -unless :test.respond_to?(:to_proc) +# Remove 1.8.7's incompatible method. +if :to_proc.respond_to?(:to_proc) && [1] != ([[1, 2]].map(&:first) rescue false) + class Symbol + remove_method :to_proc + end +end + +unless :to_proc.respond_to?(:to_proc) class Symbol # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: # -- cgit v1.2.3 From 715db1a7973dfc02466207a913bacc4702187dad Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 19 Apr 2008 16:06:30 -0700 Subject: Feature check :force_encoding instead of RUBY_VERSION --- activesupport/lib/active_support/core_ext/string/access.rb | 2 +- activesupport/lib/active_support/core_ext/string/unicode.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index 89999ff136..1a949c0b77 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -1,7 +1,7 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module String #:nodoc: - if RUBY_VERSION < '1.9' + unless '1.9'.respond_to?(:force_encoding) # Makes it easier to access parts of a string, such as specific characters and substrings. module Access # Returns the character at the +position+ treating the string as an array (where 0 is the first character). diff --git a/activesupport/lib/active_support/core_ext/string/unicode.rb b/activesupport/lib/active_support/core_ext/string/unicode.rb index 0e9770af25..963920d4a6 100644 --- a/activesupport/lib/active_support/core_ext/string/unicode.rb +++ b/activesupport/lib/active_support/core_ext/string/unicode.rb @@ -1,7 +1,7 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module String #:nodoc: - if RUBY_VERSION < '1.9' + unless '1.9'.respond_to?(:force_encoding) # Define methods for handling unicode data. module Unicode # +chars+ is a Unicode safe proxy for string methods. It creates and returns an instance of the -- cgit v1.2.3 From f67b070facef6574df3c4386e3975242273fe456 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 19 Apr 2008 17:16:19 -0700 Subject: Override incompatible 1.8.7p1 String#chars --- activesupport/lib/active_support/core_ext/string/unicode.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/string/unicode.rb b/activesupport/lib/active_support/core_ext/string/unicode.rb index 963920d4a6..1f50773505 100644 --- a/activesupport/lib/active_support/core_ext/string/unicode.rb +++ b/activesupport/lib/active_support/core_ext/string/unicode.rb @@ -4,6 +4,12 @@ module ActiveSupport #:nodoc: unless '1.9'.respond_to?(:force_encoding) # Define methods for handling unicode data. module Unicode + def self.included(base) + if '1.8.7'.respond_to?(:chars) + base.class_eval { remove_method :chars } + end + end + # +chars+ is a Unicode safe proxy for string methods. It creates and returns an instance of the # ActiveSupport::Multibyte::Chars class which encapsulates the original string. A Unicode safe version of all # the String methods are defined on this proxy class. Undefined methods are forwarded to String, so all of the -- cgit v1.2.3 From daab53d8c0bca1114c1485d0dcc52858a264ab9e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 20 Apr 2008 03:52:13 -0700 Subject: Incompatible Symbol#to_proc was reverted upstream --- activesupport/lib/active_support/core_ext/symbol.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/symbol.rb b/activesupport/lib/active_support/core_ext/symbol.rb index d41d47e70e..e4ac443809 100644 --- a/activesupport/lib/active_support/core_ext/symbol.rb +++ b/activesupport/lib/active_support/core_ext/symbol.rb @@ -1,10 +1,3 @@ -# Remove 1.8.7's incompatible method. -if :to_proc.respond_to?(:to_proc) && [1] != ([[1, 2]].map(&:first) rescue false) - class Symbol - remove_method :to_proc - end -end - unless :to_proc.respond_to?(:to_proc) class Symbol # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: -- cgit v1.2.3 From 46ab7422d9ebac0d529f71a3a7c2feaf0b9d5dbd Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Apr 2008 11:45:44 -0500 Subject: Use define_callbacks helper for ActiveRecord validations. --- activesupport/lib/active_support/callbacks.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 5c51237049..9c59b7ac76 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -99,8 +99,8 @@ module ActiveSupport def |(chain) if chain.is_a?(CallbackChain) chain.each { |callback| self | callback } - elsif chain.is_a?(Callback) - if index = index(chain) + else + if (found_callback = find(chain)) && (index = index(chain)) self[index] = chain else self << chain @@ -224,8 +224,8 @@ module ActiveSupport end end - # Runs all the callbacks defined for the given options. - # + # Runs all the callbacks defined for the given options. + # # If a block is given it will be called after each callback receiving as arguments: # # * the result from the callback @@ -236,31 +236,31 @@ module ActiveSupport # Example: # class Storage # include ActiveSupport::Callbacks - # + # # define_callbacks :before_save, :after_save # end - # + # # class ConfigStorage < Storage # before_save :pass # before_save :pass # before_save :stop # before_save :pass - # + # # def pass # puts "pass" # end - # + # # def stop # puts "stop" # return false # end - # + # # def save # result = run_callbacks(:before_save) { |result, object| result == false } # puts "- save" if result # end # end - # + # # config = ConfigStorage.new # config.save # -- cgit v1.2.3 From 59f5d4fc79b72a2bf0f7ffada4a1a73eb303d943 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 20 Apr 2008 16:49:58 -0700 Subject: Use append_features instead of included to get the inclusion order right --- activesupport/lib/active_support/core_ext/string/unicode.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/string/unicode.rb b/activesupport/lib/active_support/core_ext/string/unicode.rb index 1f50773505..ba16d4d866 100644 --- a/activesupport/lib/active_support/core_ext/string/unicode.rb +++ b/activesupport/lib/active_support/core_ext/string/unicode.rb @@ -4,10 +4,11 @@ module ActiveSupport #:nodoc: unless '1.9'.respond_to?(:force_encoding) # Define methods for handling unicode data. module Unicode - def self.included(base) + def self.append_features(base) if '1.8.7'.respond_to?(:chars) base.class_eval { remove_method :chars } end + super end # +chars+ is a Unicode safe proxy for string methods. It creates and returns an instance of the -- cgit v1.2.3 From 549c81db4a9ca941ea65ae2edafb0f34784f12f2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 20 Apr 2008 17:52:21 -0700 Subject: Ruby 1.8.7 compat: String#start_with? and #end_with? --- activesupport/lib/active_support/core_ext/string.rb | 13 +++---------- .../lib/active_support/core_ext/string/iterators.rb | 4 ++++ .../active_support/core_ext/string/starts_ends_with.rb | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) (limited to 'activesupport/lib') 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 -- cgit v1.2.3 From 0b21ac5118c293b43e5370d7a3da980514f089a2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 20 Apr 2008 19:16:48 -0700 Subject: Ruby 1.8.7 compat: override unordered Enumerable#group_by --- activesupport/lib/active_support/core_ext/enumerable.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') 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: # -- cgit v1.2.3 From 1d18651ea36e2ba274b6c2e0fd4edd425e028589 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 20 Apr 2008 19:26:46 -0700 Subject: Ruby 1.8.7 compat: detect and alias non-superclass DateTime#to_s --- .../lib/active_support/core_ext/date_time/conversions.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'activesupport/lib') 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. -- cgit v1.2.3 From 32b82e4c6f5523cdf5ee78c3022c50b46e018351 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sun, 20 Apr 2008 21:57:04 -0500 Subject: Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now --- activesupport/lib/active_support/core_ext/time/zones.rb | 5 +++++ activesupport/lib/active_support/duration.rb | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index d2b95698e5..3ffc71407c 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -35,6 +35,11 @@ module ActiveSupport #:nodoc: ::Time.zone = old_zone end + # Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now. + def current + ::Time.zone_default ? ::Time.zone.now : ::Time.now + end + private def get_zone(time_zone) return time_zone if time_zone.nil? || time_zone.is_a?(TimeZone) diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index ff51b49dcf..8eae85d38e 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -51,14 +51,14 @@ module ActiveSupport # Calculates a new Time or Date that is as far in the future # as this Duration represents. - def since(time = ::Time.now) + def since(time = ::Time.current) sum(1, time) end alias :from_now :since # Calculates a new Time or Date that is as far in the past # as this Duration represents. - def ago(time = ::Time.now) + def ago(time = ::Time.current) sum(-1, time) end alias :until :ago @@ -73,7 +73,7 @@ module ActiveSupport protected - def sum(sign, time = ::Time.now) #:nodoc: + def sum(sign, time = ::Time.current) #:nodoc: parts.inject(time) do |t,(type,number)| if t.acts_like?(:time) || t.acts_like?(:date) if type == :seconds -- cgit v1.2.3 From 6dbe90dd9b287ca383de9bae371614352a8646e7 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 21 Apr 2008 12:03:01 +0100 Subject: Alias Object#instance_variable_names to Object#instance_variables for Ruby 1.8.x --- .../lib/active_support/core_ext/object/instance_variables.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/instance_variables.rb b/activesupport/lib/active_support/core_ext/object/instance_variables.rb index ee1010b250..9f1d4ed2aa 100644 --- a/activesupport/lib/active_support/core_ext/object/instance_variables.rb +++ b/activesupport/lib/active_support/core_ext/object/instance_variables.rb @@ -33,8 +33,12 @@ class Object # end # # C.new(0, 1).instance_variable_names # => ["@y", "@x"] - def instance_variable_names - instance_variables.map(&:to_s) + if RUBY_VERSION >= '1.9' + def instance_variable_names + instance_variables.map(&:to_s) + end + else + alias_method :instance_variable_names, :instance_variables end # Copies the instance variables of +object+ into +self+. -- cgit v1.2.3