aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md6
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb7
-rw-r--r--activesupport/lib/active_support/dependencies.rb19
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb9
4 files changed, 32 insertions, 9 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index be829222ff..7f1103a6d7 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,7 +1,9 @@
## Rails 3.2.0 (unreleased) ##
-* Module#synchronize is deprecated with no replacement. Please use `monitor`
- from ruby's standard library.
+* Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*
+
+* Module#synchronize is deprecated with no replacement. Please use `monitor`
+ from ruby's standard library.
* (Date|DateTime|Time)#beginning_of_week accept an optional argument to
be able to set the day at which weeks are assumed to start.
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 9343bb7106..d9856f2e84 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -63,6 +63,13 @@ module Enumerable
end
end
+ # Plucks the value of the passed method for each element and returns the result as an array. Example:
+ #
+ # people.pluck(:name) # => [ "David Heinemeier Hansson", "Jamie Heinemeier Hansson" ]
+ def pluck(method)
+ collect { |element| element.send(method) }
+ end
+
# Iterates over a collection, passing the current element *and* the
# +memo+ to the block. Handy for building up hashes or
# reducing collections down to one object. Examples:
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb
index 1372e71a61..100b48312a 100644
--- a/activesupport/lib/active_support/dependencies.rb
+++ b/activesupport/lib/active_support/dependencies.rb
@@ -71,14 +71,20 @@ module ActiveSupport #:nodoc:
#
# This is handled by walking back up the watch stack and adding the constants
# found by child.rb to the list of original constants in parent.rb
- class WatchStack < Hash
+ class WatchStack
+ include Enumerable
+
# @watching is a stack of lists of constants being watched. For instance,
# if parent.rb is autoloaded, the stack will look like [[Object]]. If parent.rb
# then requires namespace/child.rb, the stack will look like [[Object], [Namespace]].
def initialize
@watching = []
- super { |h,k| h[k] = [] }
+ @stack = Hash.new { |h,k| h[k] = [] }
+ end
+
+ def each(&block)
+ @stack.each(&block)
end
# return a list of new constants found since the last call to watch_namespaces
@@ -89,7 +95,7 @@ module ActiveSupport #:nodoc:
@watching.last.each do |namespace|
# Retrieve the constants that were present under the namespace when watch_namespaces
# was originally called
- original_constants = self[namespace].last
+ original_constants = @stack[namespace].last
mod = Inflector.constantize(namespace) if Dependencies.qualified_const_defined?(namespace)
next unless mod.is_a?(Module)
@@ -102,7 +108,7 @@ module ActiveSupport #:nodoc:
# element of self[Object] will be an Array of the constants that were present
# before parent.rb was required. The second element will be an Array of the
# constants that were present before child.rb was required.
- self[namespace].each do |namespace_constants|
+ @stack[namespace].each do |namespace_constants|
namespace_constants.concat(new_constants)
end
@@ -126,13 +132,14 @@ module ActiveSupport #:nodoc:
Inflector.constantize(module_name).local_constant_names : []
watching << module_name
- self[module_name] << original_constants
+ @stack[module_name] << original_constants
end
@watching << watching
end
+ private
def pop_modules(modules)
- modules.each { |mod| self[mod].pop }
+ modules.each { |mod| @stack[mod].pop }
end
end
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index cdfa991a34..7ce7c4d52d 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -126,4 +126,11 @@ class EnumerableTests < Test::Unit::TestCase
assert_equal true, GenericEnumerable.new([ 1 ]).exclude?(2)
assert_equal false, GenericEnumerable.new([ 1 ]).exclude?(1)
end
-end
+
+ def test_pluck_single_method
+ person = Struct.new(:name)
+ people = [ person.new("David"), person.new("Jamie") ]
+
+ assert_equal [ "David", "Jamie" ], people.pluck(:name)
+ end
+end \ No newline at end of file