From 5d27338ab08496b41ef71c789e5ae4de0b3b8df7 Mon Sep 17 00:00:00 2001 From: Mikhail Dieterle Date: Mon, 9 Jul 2012 00:10:16 +0300 Subject: make Hash#extract! more symmetric with Hash#slice --- activesupport/CHANGELOG.md | 11 +++++++++++ activesupport/lib/active_support/core_ext/hash/slice.rb | 6 +++--- activesupport/test/core_ext/hash_ext_test.rb | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 0a12ba6cdd..82752b6776 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,16 @@ ## Rails 4.0.0 (unreleased) ## +* Hash#extract! returns only those keys that present in the reciever. + + {:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1} + + *Mikhail Dieterle* + +* Hash#extract! returns the same subclass, that the reciever is. I.e. + HashWithIndifferentAccess#extract! returns HashWithIndifferentAccess instance. + + *Mikhail Dieterle* + * Optimize ActiveSupport::Cache::Entry to reduce memory and processing overhead. *Brian Durand* * Tests tag the Rails log with the current test class and test case: diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 45fec57009..de09f26f3d 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -32,9 +32,9 @@ class Hash # Removes and returns the key/value pairs matching the given keys. # - # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) - # # => {:a => 1, :b => 2} + # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => { a: 1, b: 2 } + # { a: 1, b: 2 }.extract!(:a, :x) # => { a: 1 } def extract!(*keys) - keys.each_with_object({}) { |key, result| result[key] = delete(key) } + keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) } end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 01934dd2c3..5ba8e822b3 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -725,6 +725,23 @@ class HashExtTest < ActiveSupport::TestCase original = {:a => 1, :b => 2, :c => 3, :d => 4} expected = {:a => 1, :b => 2} + assert_equal expected, original.extract!(:a, :b, :x) + end + + def test_extract_nils + original = {:a => nil, :b => nil} + expected = {:a => nil} + extracted = original.extract!(:a, :x) + + assert_equal expected, extracted + assert_equal nil, extracted[:a] + assert_equal nil, extracted[:x] + end + + def test_indifferent_extract + original = {:a => 1, :b => 2, :c => 3, :d => 4}.with_indifferent_access + expected = {:a => 1, :b => 2}.with_indifferent_access + assert_equal expected, original.extract!(:a, :b) end -- cgit v1.2.3 From a4b11961630febd556c962b788b11c0ed5bedb45 Mon Sep 17 00:00:00 2001 From: Mikhail Dieterle Date: Fri, 21 Sep 2012 11:43:59 +0300 Subject: add more testcases and doc about Hash#extract! --- activesupport/CHANGELOG.md | 4 ++-- activesupport/test/core_ext/hash_ext_test.rb | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 82752b6776..23e2ce0b03 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,12 +1,12 @@ ## Rails 4.0.0 (unreleased) ## -* Hash#extract! returns only those keys that present in the reciever. +* Hash#extract! returns only those keys that present in the receiver. {:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1} *Mikhail Dieterle* -* Hash#extract! returns the same subclass, that the reciever is. I.e. +* Hash#extract! returns the same subclass, that the receiver is. I.e. HashWithIndifferentAccess#extract! returns HashWithIndifferentAccess instance. *Mikhail Dieterle* diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 5ba8e822b3..7cfe7b0ea7 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -724,8 +724,10 @@ class HashExtTest < ActiveSupport::TestCase def test_extract original = {:a => 1, :b => 2, :c => 3, :d => 4} expected = {:a => 1, :b => 2} + remaining = {:c => 3, :d => 4} assert_equal expected, original.extract!(:a, :b, :x) + assert_equal remaining, original end def test_extract_nils @@ -739,10 +741,15 @@ class HashExtTest < ActiveSupport::TestCase end def test_indifferent_extract - original = {:a => 1, :b => 2, :c => 3, :d => 4}.with_indifferent_access + original = {:a => 1, 'b' => 2, :c => 3, 'd' => 4}.with_indifferent_access expected = {:a => 1, :b => 2}.with_indifferent_access + remaining = {:c => 3, :d => 4}.with_indifferent_access - assert_equal expected, original.extract!(:a, :b) + [['a', 'b'], [:a, :b]].each do |keys| + copy = original.dup + assert_equal expected, copy.extract!(*keys) + assert_equal remaining, copy + end end def test_except -- cgit v1.2.3 From 775829869e23666e114edbe8d6d3ff08d5d71025 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 12 Oct 2012 18:01:04 -0700 Subject: Remove the queue container. Premature consolidation. Set up and maintain queues in the classes that use them instead. --- activesupport/lib/active_support/queueing.rb | 26 ------------------------- activesupport/test/queueing/container_test.rb | 28 --------------------------- 2 files changed, 54 deletions(-) delete mode 100644 activesupport/test/queueing/container_test.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/queueing.rb b/activesupport/lib/active_support/queueing.rb index 7c352886f3..430ecbe323 100644 --- a/activesupport/lib/active_support/queueing.rb +++ b/activesupport/lib/active_support/queueing.rb @@ -56,32 +56,6 @@ module ActiveSupport end end - # A container for multiple queues. This class delegates to a default Queue - # so that Rails.queue.push and friends will Just Work. To use this class - # with multiple queues: - # - # # In your configuration: - # Rails.queue[:image_queue] = SomeQueue.new - # Rails.queue[:mail_queue] = SomeQueue.new - # - # # In your app code: - # Rails.queue[:mail_queue].push SomeJob.new - # - class QueueContainer < DelegateClass(::Queue) - def initialize(default_queue) - @queues = { :default => default_queue } - super(default_queue) - end - - def [](queue_name) - @queues[queue_name] - end - - def []=(queue_name, queue) - @queues[queue_name] = queue - end - end - # The threaded consumer will run jobs in a background thread in # development mode or in a VM where running jobs on a thread in # production mode makes sense. diff --git a/activesupport/test/queueing/container_test.rb b/activesupport/test/queueing/container_test.rb deleted file mode 100644 index 7afc11e7a9..0000000000 --- a/activesupport/test/queueing/container_test.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'abstract_unit' -require 'active_support/queueing' - -module ActiveSupport - class ContainerTest < ActiveSupport::TestCase - def test_delegates_to_default - q = Queue.new - container = QueueContainer.new q - job = Object.new - - container.push job - assert_equal job, q.pop - end - - def test_access_default - q = Queue.new - container = QueueContainer.new q - assert_equal q, container[:default] - end - - def test_assign_queue - container = QueueContainer.new Object.new - q = Object.new - container[:foo] = q - assert_equal q, container[:foo] - end - end -end -- cgit v1.2.3 From c8fe0d58bcc7deb49711606566c854bf0ed7f107 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 12 Oct 2012 20:45:54 -0700 Subject: Backpedal from class-oriented config.queue. Set an actual queue instance. --- activesupport/lib/active_support/queueing.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/queueing.rb b/activesupport/lib/active_support/queueing.rb index 430ecbe323..c8ba28021d 100644 --- a/activesupport/lib/active_support/queueing.rb +++ b/activesupport/lib/active_support/queueing.rb @@ -64,10 +64,6 @@ module ActiveSupport # queue and joins the thread, which will ensure that all jobs # are executed before the process finally dies. class ThreadedQueueConsumer - def self.start(*args) - new(*args).start - end - def initialize(queue, options = {}) @queue = queue @logger = options[:logger] -- cgit v1.2.3