aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-26 01:56:52 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-26 01:56:52 +0100
commitabad097016bf5243e9812f6a031f421a986b09f7 (patch)
treef526a520a87db9c68ddd3436ce4bdb6f8c3182d1 /activesupport
parent8974dac92e05dcab8ee552a5f40108c6ac25dc36 (diff)
parentc02391f8f97182e818d22a0f0ec4a5589d2fff15 (diff)
downloadrails-abad097016bf5243e9812f6a031f421a986b09f7.tar.gz
rails-abad097016bf5243e9812f6a031f421a986b09f7.tar.bz2
rails-abad097016bf5243e9812f6a031f421a986b09f7.zip
Merge remote branch 'mikel/master'
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/array.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/array/uniq_by.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/hash/deep_merge.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb15
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb40
6 files changed, 52 insertions, 47 deletions
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/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
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
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
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index f5f91ddd80..d4cd5ddbde 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -52,8 +52,6 @@ class ArrayExtToParamTests < Test::Unit::TestCase
end
class ArrayExtToSentenceTests < Test::Unit::TestCase
- include ActiveSupport::Testing::Deprecation
-
def test_plain_array_to_sentence
assert_equal "", [].to_sentence
assert_equal "one", ['one'].to_sentence
@@ -62,28 +60,12 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
end
def test_to_sentence_with_words_connector
- assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
- assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
- end
-
- assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
- assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
- end
-
assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
end
def test_to_sentence_with_last_word_connector
- assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
- assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true)
- end
-
- assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
- assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
- end
-
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
@@ -320,6 +302,28 @@ class ArrayExtractOptionsTests < Test::Unit::TestCase
end
end
+class ArrayUniqByTests < Test::Unit::TestCase
+ def test_uniq_by
+ assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? }
+ assert_equal [1,2], [1,2,3,4].uniq_by(&:even?)
+ assert_equal (-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 }
+ end
+
+ def test_uniq_by!
+ a = [1,2,3,4]
+ a.uniq_by! { |i| i.odd? }
+ assert_equal [1,2], a
+
+ a = [1,2,3,4]
+ a.uniq_by! { |i| i.even? }
+ assert_equal [1,2], a
+
+ a = (-5..5).to_a
+ a.uniq_by! { |i| i**2 }
+ assert_equal (-5..0).to_a, a
+ end
+end
+
class ArrayExtRandomTests < Test::Unit::TestCase
def test_random_element_from_array
assert_nil [].rand