From 29096268ccce2b13e1490c8b673ffe0b498555fc Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 14 Apr 2009 01:12:55 -0400 Subject: Enumerable#sum now works will all enumerables, even if they don't respond to :size [#2489 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/core_ext/enumerable.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (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 434a32b29b..e89b7e392e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -55,12 +55,10 @@ module Enumerable # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0) # def sum(identity = 0, &block) - return identity unless size > 0 - if block_given? - map(&block).sum + map(&block).sum(identity) else - inject { |sum, element| sum + element } + inject { |sum, element| sum + element } || identity end end -- cgit v1.2.3 From b05c95190737eb39047a756e37d811b340a47ba2 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 03:29:56 -0300 Subject: Temporary fix to get our LoadError monkey-patch working with newer JRuby. We should probably remove MissingSourceFile and just monkey-patch LoadError instead of overriding LoadError.new. --- activesupport/lib/active_support/core_ext/load_error.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/load_error.rb b/activesupport/lib/active_support/core_ext/load_error.rb index f36a21818f..cc6287b100 100644 --- a/activesupport/lib/active_support/core_ext/load_error.rb +++ b/activesupport/lib/active_support/core_ext/load_error.rb @@ -20,7 +20,8 @@ class MissingSourceFile < LoadError #:nodoc: REGEXPS = [ [/^no such file to load -- (.+)$/i, 1], [/^Missing \w+ (file\s*)?([^\s]+.rb)$/i, 2], - [/^Missing API definition file in (.+)$/i, 1] + [/^Missing API definition file in (.+)$/i, 1], + [/win32/, 0] ] unless defined?(REGEXPS) end -- cgit v1.2.3 From d811864e880580549bc1ab73a1ab0be886598e6e Mon Sep 17 00:00:00 2001 From: Michael Siebert Date: Thu, 25 Jun 2009 00:59:58 +0200 Subject: Fix deprecating =-methods by using send [#2431 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/active_support/deprecation/method_wrappers.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb index b9eb539aa7..deb29a82b8 100644 --- a/activesupport/lib/active_support/deprecation/method_wrappers.rb +++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb @@ -11,15 +11,15 @@ module ActiveSupport method_names.each do |method_name| target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation| target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1) - def #{target}_with_deprecation#{punctuation}(*args, &block) # def generate_secret_with_deprecation(*args, &block) - ::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn( - ::ActiveSupport::Deprecation.deprecated_method_warning( # ::ActiveSupport::Deprecation.deprecated_method_warning( - :#{method_name}, # :generate_secret, - #{options[method_name].inspect}), # "You should use ActiveSupport::SecureRandom.hex(64)"), - caller # caller - ) # ) - #{target}_without_deprecation#{punctuation}(*args, &block) # generate_secret_without_deprecation(*args, &block) - end # end + def #{target}_with_deprecation#{punctuation}(*args, &block) # def generate_secret_with_deprecation(*args, &block) + ::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn( + ::ActiveSupport::Deprecation.deprecated_method_warning( # ::ActiveSupport::Deprecation.deprecated_method_warning( + :#{method_name}, # :generate_secret, + #{options[method_name].inspect}), # "You should use ActiveSupport::SecureRandom.hex(64)"), + caller # caller + ) # ) + send(:#{target}_without_deprecation#{punctuation}, *args, &block) # send(:generate_secret_without_deprecation, *args, &block) + end # end end_eval end end -- cgit v1.2.3 From 1bd4d1c67459a91415ee73a8f55d2309c0d62a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 9 Aug 2009 13:22:06 +0200 Subject: Optimize Range#sum to use arithmetic progression when a block is not given [#2489]. Signed-off-by: Pratik Naik --- activesupport/lib/active_support/core_ext/enumerable.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (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 e89b7e392e..d68eef8c23 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -111,3 +111,12 @@ module Enumerable !any?(&block) end unless [].respond_to?(:none?) end + +class Range #:nodoc: + # Optimize range sum to use arithmetic progression if a block is not given. + def sum(identity=0, &block) + return super if block_given? + actual_last = exclude_end? ? (last - 1) : last + (actual_last - first + 1) * (actual_last + first) / 2 + end +end -- cgit v1.2.3 From 3545d6b0b227cc9ee4a645097bdbd43b33b42a87 Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Sun, 9 Aug 2009 15:19:56 +0200 Subject: Setting usec (and nsec for Ruby 1.9) on Time#end_of_* methods [#1255 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/active_support/core_ext/time/calculations.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index b73c3b2c9b..4f3b869f50 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -158,7 +158,7 @@ class Time alias :monday :beginning_of_week alias :at_beginning_of_week :beginning_of_week - # Returns a new Time representing the end of this week (Sunday, 23:59:59) + # Returns a new Time representing the end of this week, (end of Sunday) def end_of_week days_to_sunday = wday!=0 ? 7-wday : 0 (self + days_to_sunday.days).end_of_day @@ -178,9 +178,9 @@ class Time alias :at_midnight :beginning_of_day alias :at_beginning_of_day :beginning_of_day - # Returns a new Time representing the end of the day (23:59:59) + # Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9) def end_of_day - change(:hour => 23, :min => 59, :sec => 59) + change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end # Returns a new Time representing the start of the month (1st of the month, 0:00) @@ -190,11 +190,11 @@ class Time end alias :at_beginning_of_month :beginning_of_month - # Returns a new Time representing the end of the month (last day of the month, 0:00) + # Returns a new Time representing the end of the month (end of the last day of the month) def end_of_month #self - ((self.mday-1).days + self.seconds_since_midnight) last_day = ::Time.days_in_month(month, year) - change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0) + change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end alias :at_end_of_month :end_of_month @@ -204,7 +204,7 @@ class Time end alias :at_beginning_of_quarter :beginning_of_quarter - # Returns a new Time representing the end of the quarter (last day of march, june, september, december, 23:59:59) + # Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december) def end_of_quarter beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= month }).end_of_month end @@ -216,9 +216,9 @@ class Time end alias :at_beginning_of_year :beginning_of_year - # Returns a new Time representing the end of the year (31st of december, 23:59:59) + # Returns a new Time representing the end of the year (end of the 31st of december) def end_of_year - change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59) + change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end alias :at_end_of_year :end_of_year -- cgit v1.2.3 From e0adfa82c05f9c975005f102b4bcaebfcd17d241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 9 Aug 2009 19:03:09 +0200 Subject: Optimize Range#sum only for integers [#2489] --- activesupport/lib/active_support/core_ext/enumerable.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (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 d68eef8c23..42686cfbd5 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -113,9 +113,10 @@ module Enumerable end class Range #:nodoc: - # Optimize range sum to use arithmetic progression if a block is not given. + # Optimize range sum to use arithmetic progression if a block is not given and + # we have a range of numeric values. def sum(identity=0, &block) - return super if block_given? + return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer)) actual_last = exclude_end? ? (last - 1) : last (actual_last - first + 1) * (actual_last + first) / 2 end -- cgit v1.2.3 From 60bed59fbf21fcfe5b9a753181f6d34488055acd Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 9 Aug 2009 18:15:13 +0100 Subject: Remove unnecessary &block from Range#sum and add tests for (num..float).sum --- activesupport/lib/active_support/core_ext/enumerable.rb | 2 +- 1 file changed, 1 insertion(+), 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 42686cfbd5..15a303cf04 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -115,7 +115,7 @@ end class Range #:nodoc: # Optimize range sum to use arithmetic progression if a block is not given and # we have a range of numeric values. - def sum(identity=0, &block) + def sum(identity = 0) return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer)) actual_last = exclude_end? ? (last - 1) : last (actual_last - first + 1) * (actual_last + first) / 2 -- cgit v1.2.3 From 1382f4de1f9b0e443e7884bd4da53c20f0754568 Mon Sep 17 00:00:00 2001 From: David Burger Date: Sun, 17 May 2009 21:36:44 -0700 Subject: Fix that Hash#to_xml and Array#to_xml shouldn't modify their options hashes [#672 state:resolved] Signed-off-by: Pratik Naik --- activesupport/lib/active_support/core_ext/array/conversions.rb | 1 + activesupport/lib/active_support/core_ext/hash/conversions.rb | 1 + 2 files changed, 2 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 11846f265c..c53cf3f530 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -159,6 +159,7 @@ class Array raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } require 'builder' unless defined?(Builder) + options = options.dup options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? ActiveSupport::Inflector.pluralize(ActiveSupport::Inflector.underscore(first.class.name)) : "records" options[:children] ||= options[:root].singularize options[:indent] ||= 2 diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 2a34874d08..bd9419e1a2 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -86,6 +86,7 @@ class Hash def to_xml(options = {}) require 'builder' unless defined?(Builder) + options = options.dup options[:indent] ||= 2 options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]), :root => "hash" }) -- cgit v1.2.3 From ca92d44e7637ae6d28d6b88b67873d2795290cb5 Mon Sep 17 00:00:00 2001 From: Andrew Moreland Date: Sun, 9 Aug 2009 15:38:30 -0700 Subject: Support deep-merging HashWithIndifferentAccess. [#2732 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/core_ext/hash/deep_merge.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb index ffde34a741..24d0a2a481 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -1,11 +1,12 @@ class Hash # Returns a new hash with +self+ and +other_hash+ merged recursively. def deep_merge(other_hash) - merge(other_hash) do |key, oldval, newval| - oldval = oldval.to_hash if oldval.respond_to?(:to_hash) - newval = newval.to_hash if newval.respond_to?(:to_hash) - oldval.is_a?( Hash ) && newval.is_a?( Hash ) ? oldval.deep_merge(newval) : newval + target = dup + other_hash.each_pair do |k,v| + tv = target[k] + target[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v end + target end # Returns a new hash with +self+ and +other_hash+ merged recursively. -- cgit v1.2.3 From e4ceea3795ecc7adcec28a1b9d63782be1401256 Mon Sep 17 00:00:00 2001 From: jeem Date: Sun, 9 Aug 2009 20:04:15 -0500 Subject: make private_and_public_methods unmemoizable [#2372 state:resolved] Signed-off-by: Joshua Peek --- activesupport/lib/active_support/memoizable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index fa6db683d4..7724b9d88b 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -59,7 +59,7 @@ module ActiveSupport def flush_cache(*syms, &block) syms.each do |sym| - methods.each do |m| + (methods + private_methods + protected_methods).each do |m| if m.to_s =~ /^_unmemoized_(#{sym})/ ivar = ActiveSupport::Memoizable.memoized_ivar_for($1) instance_variable_get(ivar).clear if instance_variable_defined?(ivar) -- cgit v1.2.3 From 4dda9b644df5e4386f693a4b7bd00fe787f41a28 Mon Sep 17 00:00:00 2001 From: Erik Ostrom Date: Sun, 9 Aug 2009 18:57:25 -0700 Subject: Add rindex to ActiveSupport::Multibyte::Chars. Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/multibyte/chars.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 96ed35f0e0..3c16999e43 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -210,6 +210,19 @@ module ActiveSupport #:nodoc: index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil end + # Returns the position _needle_ in the string, counting in + # codepoints, searching backward from _offset_ or the end of the + # string. Returns +nil+ if _needle_ isn't found. + # + # Example: + # 'Café périferôl'.mb_chars.rindex('é') #=> 5 + # 'Café périferôl'.mb_chars.rindex(/\w/u) #=> 13 + def rindex(needle, offset=nil) + offset ||= length + index = @wrapped_string.rindex(needle, offset) + index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil + end + # Like String#[]=, except instead of byte offsets you specify character offsets. # # Example: -- cgit v1.2.3 From 82dd725fc195eb52eea9cbde9530ab9dff122e32 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 4 Jul 2008 14:04:50 +0700 Subject: Fix that irregular plural inflections should not be double-pluralized: 'people'.pluralize should return 'people' not 'peoples'. [#1183 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/inflector.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 4ee96b13b4..67aea2782f 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -69,10 +69,13 @@ module ActiveSupport @uncountables.delete(plural) if singular[0,1].upcase == plural[0,1].upcase plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1]) + plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1]) singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1]) else plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) + plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) + plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1]) singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1]) end -- cgit v1.2.3 From b3381cacaf7735ec6eb108e378ba255ebf6ffb14 Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Sun, 9 Aug 2009 11:27:17 +0400 Subject: Fix that JSON parser fails to read escaped backslashes. [#973 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/json/backends/yaml.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/json/backends/yaml.rb b/activesupport/lib/active_support/json/backends/yaml.rb index 92dd31cfbc..59d2c37e40 100644 --- a/activesupport/lib/active_support/json/backends/yaml.rb +++ b/activesupport/lib/active_support/json/backends/yaml.rb @@ -20,7 +20,7 @@ module ActiveSupport rescue ArgumentError => e raise ParseError, "Invalid JSON string" end - + protected # Ensure that ":" and "," are always followed by a space def convert_json_to_yaml(json) #:nodoc: @@ -42,6 +42,8 @@ module ActiveSupport end when ":","," marks << scanner.pos - 1 unless quoting + when "\\" + scanner.skip(/\\/) end end @@ -89,3 +91,4 @@ module ActiveSupport end end end + -- cgit v1.2.3 From 7d16e94d827197d030fdd611db4310396aeb0114 Mon Sep 17 00:00:00 2001 From: Erik Ostrom Date: Sun, 9 Aug 2009 22:14:59 -0700 Subject: Correctly handle offsets in Multibyte::Chars#index and #rindex. The offset in codepoints was being passed directly to the wrapped string's index/rindex method. Now we translate the offset into bytes first. [#3028 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/multibyte/chars.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 3c16999e43..64a35dca40 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -206,7 +206,8 @@ module ActiveSupport #:nodoc: # 'Café périferôl'.mb_chars.index('ô') #=> 12 # 'Café périferôl'.mb_chars.index(/\w/u) #=> 0 def index(needle, offset=0) - index = @wrapped_string.index(needle, offset) + wrapped_offset = self.first(offset).wrapped_string.length + index = @wrapped_string.index(needle, wrapped_offset) index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil end @@ -215,11 +216,12 @@ module ActiveSupport #:nodoc: # string. Returns +nil+ if _needle_ isn't found. # # Example: - # 'Café périferôl'.mb_chars.rindex('é') #=> 5 + # 'Café périferôl'.mb_chars.rindex('é') #=> 6 # 'Café périferôl'.mb_chars.rindex(/\w/u) #=> 13 def rindex(needle, offset=nil) offset ||= length - index = @wrapped_string.rindex(needle, offset) + wrapped_offset = self.first(offset).wrapped_string.length + index = @wrapped_string.rindex(needle, wrapped_offset) index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil end -- cgit v1.2.3