From 0361414ae328c10de8ed778e826d8244ba0aa63a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 23:28:52 +0100 Subject: Add uniq_by and uniq_by! to Array. --- activesupport/lib/active_support/core_ext/array.rb | 1 + .../lib/active_support/core_ext/array/uniq_by.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/array/uniq_by.rb (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index b583c7533e..4688468a8f 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/array/access' +require 'active_support/core_ext/array/uniq_by' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' diff --git a/activesupport/lib/active_support/core_ext/array/uniq_by.rb b/activesupport/lib/active_support/core_ext/array/uniq_by.rb new file mode 100644 index 0000000000..a09b2302fd --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/uniq_by.rb @@ -0,0 +1,17 @@ +class Array + # Return an unique array based on the criteria given as a proc. + # + # [1, 2, 3, 4].uniq_by { |i| i.odd? } + # #=> [1, 2] + # + def uniq_by + hash, array = {}, [] + each { |i| hash[yield(i)] ||= (array << i) } + array + end + + # Same as uniq_by, but modifies self. + def uniq_by! + replace(uniq_by{ |i| yield(i) }) + end +end -- cgit v1.2.3 From 328b0b1268596347975349c6df5ce713ad377bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 23:32:09 +0100 Subject: Remove deprecated behavior since 2.3. --- .../lib/active_support/core_ext/array/conversions.rb | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 7fcef38372..814567a5a6 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -13,19 +13,6 @@ class Array default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale]) default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale]) - # Try to emulate to_sentences previous to 2.3 - if options.has_key?(:connector) || options.has_key?(:skip_last_comma) - ::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector - ::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma - - skip_last_comma = options.delete :skip_last_comma - if connector = options.delete(:connector) - options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}" - else - options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector - end - end - options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale) options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector -- cgit v1.2.3 From c0262827cacc1baf16668af65c35a09138166394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 23:40:45 +0100 Subject: Speed up some Hash core extensions. --- .../lib/active_support/core_ext/hash/deep_merge.rb | 13 ++++++------- activesupport/lib/active_support/core_ext/hash/keys.rb | 15 ++++++--------- 2 files changed, 12 insertions(+), 16 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') 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 24d0a2a481..af771c86ff 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -1,17 +1,16 @@ class Hash # Returns a new hash with +self+ and +other_hash+ merged recursively. def deep_merge(other_hash) - 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 + dup.deep_merge!(other_hash) end # Returns a new hash with +self+ and +other_hash+ merged recursively. # Modifies the receiver in place. def deep_merge!(other_hash) - replace(deep_merge(other_hash)) + other_hash.each_pair do |k,v| + tv = self[k] + self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v + end + self end end diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index ecd63293b4..045a6944fa 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -1,10 +1,7 @@ class Hash # Return a new hash with all keys converted to strings. def stringify_keys - inject({}) do |options, (key, value)| - options[key.to_s] = value - options - end + dup.stringify_keys! end # Destructively convert all keys to strings. @@ -18,16 +15,16 @@ class Hash # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. def symbolize_keys - inject({}) do |options, (key, value)| - options[(key.to_sym rescue key) || key] = value - options - end + dup.symbolize_keys! end # Destructively convert all keys to symbols, as long as they respond # to +to_sym+. def symbolize_keys! - self.replace(self.symbolize_keys) + keys.each do |key| + self[(key.to_sym rescue key) || key] = delete(key) + end + self end alias_method :to_options, :symbolize_keys -- cgit v1.2.3