diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
16 files changed, 84 insertions, 61 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 426e53ea1c..8205843a2b 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -25,12 +25,14 @@ module ActiveSupport #:nodoc: def to_param join '/' end - - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + + def self.included(base) #:nodoc: + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end - + def to_formatted_s(format = :default) case format when :db diff --git a/activesupport/lib/active_support/core_ext/class/removal.rb b/activesupport/lib/active_support/core_ext/class/removal.rb index b217c1957c..0c70e7181a 100644 --- a/activesupport/lib/active_support/core_ext/class/removal.rb +++ b/activesupport/lib/active_support/core_ext/class/removal.rb @@ -17,8 +17,8 @@ class Class #:nodoc: # Skip this class if it does not match the current one bound to this name next unless parent.const_defined?(basename) && klass = parent.const_get(basename) - - parent.send :remove_const, basename unless parent == klass + + parent.instance_eval { remove_const basename } unless parent == klass end end end diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index c28d0c876b..37f5d3b544 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -4,13 +4,15 @@ module ActiveSupport #:nodoc: # Enables the use of time calculations within Time itself module Calculations def self.included(base) #:nodoc: - base.send(:extend, ClassMethods) - - base.send(:alias_method, :plus_without_duration, :+) - base.send(:alias_method, :+, :plus_with_duration) - - base.send(:alias_method, :minus_without_duration, :-) - base.send(:alias_method, :-, :minus_with_duration) + base.extend ClassMethods + + base.instance_eval do + alias_method :plus_without_duration, :+ + alias_method :+, :plus_with_duration + + alias_method :minus_without_duration, :- + alias_method :-, :minus_with_duration + end end module ClassMethods @@ -199,4 +201,4 @@ module ActiveSupport #:nodoc: end end end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 6e864aee5e..3491dec25a 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -11,11 +11,13 @@ module ActiveSupport #:nodoc: :rfc822 => "%e %b %Y" } - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) - klass.send(:alias_method, :default_inspect, :inspect) - klass.send(:alias_method, :inspect, :readable_inspect) + def self.included(base) #:nodoc: + base.instance_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + alias_method :default_inspect, :inspect + alias_method :inspect, :readable_inspect + end end def to_formatted_s(format = :default) diff --git a/activesupport/lib/active_support/core_ext/date_time.rb b/activesupport/lib/active_support/core_ext/date_time.rb index c99d1283fd..7ce1b78090 100644 --- a/activesupport/lib/active_support/core_ext/date_time.rb +++ b/activesupport/lib/active_support/core_ext/date_time.rb @@ -3,6 +3,8 @@ require "#{File.dirname(__FILE__)}/time/behavior" require "#{File.dirname(__FILE__)}/date_time/calculations" require "#{File.dirname(__FILE__)}/date_time/conversions" -DateTime.send(:include, ActiveSupport::CoreExtensions::Time::Behavior) -DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Calculations) -DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Conversions) +class DateTime + include ActiveSupport::CoreExtensions::Time::Behavior + include ActiveSupport::CoreExtensions::DateTime::Calculations + include ActiveSupport::CoreExtensions::DateTime::Conversions +end 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 ce0bfd8e0a..91ed0e94e9 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -3,11 +3,13 @@ module ActiveSupport #:nodoc: module DateTime #:nodoc: # Getting datetimes in different convenient string representations and other objects module Conversions - def self.included(klass) - klass.send(:alias_method, :to_datetime_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) - klass.send(:alias_method, :default_inspect, :inspect) - klass.send(:alias_method, :inspect, :readable_inspect) + def self.included(base) + base.class_eval do + alias_method :to_datetime_default_s, :to_s + alias_method :to_s, :to_formatted_s + alias_method :default_inspect, :inspect + alias_method :inspect, :readable_inspect + end end def to_formatted_s(format = :default) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 93372bb4d5..f35c8f86c5 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -38,6 +38,7 @@ module Enumerable # def sum(identity = 0, &block) return identity unless size > 0 + if block_given? map(&block).sum else diff --git a/activesupport/lib/active_support/core_ext/exception.rb b/activesupport/lib/active_support/core_ext/exception.rb index ec15a915be..14cd577066 100644 --- a/activesupport/lib/active_support/core_ext/exception.rb +++ b/activesupport/lib/active_support/core_ext/exception.rb @@ -8,8 +8,8 @@ class Exception # :nodoc: def clean_backtrace backtrace.collect do |line| - Pathname.clean_within(TraceSubstitutions.inject(line) do |line, (regexp, sub)| - line.gsub regexp, sub + Pathname.clean_within(TraceSubstitutions.inject(line) do |result, (regexp, sub)| + result.gsub regexp, sub end) end end diff --git a/activesupport/lib/active_support/core_ext/float/rounding.rb b/activesupport/lib/active_support/core_ext/float/rounding.rb index 989b1f8f80..062d466838 100644 --- a/activesupport/lib/active_support/core_ext/float/rounding.rb +++ b/activesupport/lib/active_support/core_ext/float/rounding.rb @@ -3,8 +3,10 @@ module ActiveSupport #:nodoc: module Float #:nodoc: module Rounding def self.included(base) #:nodoc: - base.send(:alias_method, :round_without_precision, :round) - base.send(:alias_method, :round, :round_with_precision) + base.class_eval do + alias_method :round_without_precision, :round + alias_method :round, :round_with_precision + end end # Rounds the float with the specified precision. diff --git a/activesupport/lib/active_support/core_ext/name_error.rb b/activesupport/lib/active_support/core_ext/name_error.rb index 4af8f22821..49176c12d9 100644 --- a/activesupport/lib/active_support/core_ext/name_error.rb +++ b/activesupport/lib/active_support/core_ext/name_error.rb @@ -1,5 +1,5 @@ # Add a +missing_name+ method to NameError instances. -class NameError < StandardError #:nodoc: +class NameError #:nodoc: # Add a method to obtain the missing name from a NameError. def missing_name $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message @@ -14,4 +14,4 @@ class NameError < StandardError #:nodoc: missing_name == name.to_s end end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index ea893c54b8..b93db689a7 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -51,4 +51,4 @@ class Object def acts_like?(duck) respond_to? "acts_like_#{duck}?" end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb index 677ba639ec..77e86a6ae6 100644 --- a/activesupport/lib/active_support/core_ext/range/conversions.rb +++ b/activesupport/lib/active_support/core_ext/range/conversions.rb @@ -6,16 +6,18 @@ module ActiveSupport #:nodoc: DATE_FORMATS = { :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" } } - - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + + def self.included(base) #:nodoc: + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end - + def to_formatted_s(format = :default) DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s end end end end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index b9f075522f..ef2f334fe2 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/string/inflections' require File.dirname(__FILE__) + '/string/conversions' require File.dirname(__FILE__) + '/string/access' require File.dirname(__FILE__) + '/string/starts_ends_with' -require File.dirname(__FILE__) + '/string/iterators' +require File.dirname(__FILE__) + '/string/iterators' unless 'test'.respond_to?(:each_char) require File.dirname(__FILE__) + '/string/unicode' class String #:nodoc: @@ -10,6 +10,8 @@ class String #:nodoc: include ActiveSupport::CoreExtensions::String::Conversions include ActiveSupport::CoreExtensions::String::Inflections include ActiveSupport::CoreExtensions::String::StartsEndsWith - include ActiveSupport::CoreExtensions::String::Iterators + if defined? ActiveSupport::CoreExtensions::String::Iterators + include ActiveSupport::CoreExtensions::String::Iterators + end include ActiveSupport::CoreExtensions::String::Unicode end diff --git a/activesupport/lib/active_support/core_ext/symbol.rb b/activesupport/lib/active_support/core_ext/symbol.rb index a3dc794db4..54f541ad9a 100644 --- a/activesupport/lib/active_support/core_ext/symbol.rb +++ b/activesupport/lib/active_support/core_ext/symbol.rb @@ -1,12 +1,14 @@ -class Symbol - # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: - # - # # The same as people.collect { |p| p.name } - # people.collect(&:name) - # - # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } - # people.select(&:manager?).collect(&:salary) - def to_proc - Proc.new { |*args| args.shift.__send__(self, *args) } +unless :test.respond_to?(:to_proc) + class Symbol + # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: + # + # # The same as people.collect { |p| p.name } + # people.collect(&:name) + # + # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } + # people.select(&:manager?).collect(&:salary) + def to_proc + Proc.new { |*args| args.shift.__send__(self, *args) } + end end end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 507bb5f180..e5302f990b 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -4,12 +4,14 @@ module ActiveSupport #:nodoc: # Enables the use of time calculations within Time itself module Calculations def self.included(base) #:nodoc: - base.extend(ClassMethods) + base.extend ClassMethods - base.send(:alias_method, :plus_without_duration, :+) - base.send(:alias_method, :+, :plus_with_duration) - base.send(:alias_method, :minus_without_duration, :-) - base.send(:alias_method, :-, :minus_with_duration) + base.class_eval do + alias_method :plus_without_duration, :+ + alias_method :+, :plus_with_duration + alias_method :minus_without_duration, :- + alias_method :-, :minus_with_duration + end end module ClassMethods diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 7829c2e581..742f71e0a6 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -12,9 +12,11 @@ module ActiveSupport #:nodoc: :rfc822 => "%a, %d %b %Y %H:%M:%S %z" } - def self.included(klass) - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + def self.included(base) + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end def to_formatted_s(format = :default) |