This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Changes b/Changes | |
index 5bdcc76..8744503 100644 | |
--- a/Changes | |
+++ b/Changes | |
@@ -1,5 +1,7 @@ | |
-5.48 2014-10-04 | |
+5.48 2014-10-05 | |
+ - Improved pluck method in Mojo::Collection to be able to extract values | |
+ from hash references. | |
5.47 2014-09-28 | |
- Improved url_for performance. | |
diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm | |
index 9c112d1..4adabd4 100644 | |
--- a/lib/Mojo/Collection.pm | |
+++ b/lib/Mojo/Collection.pm | |
@@ -70,7 +70,8 @@ sub new { | |
sub pluck { | |
my ($self, $method, @args) = @_; | |
- return $self->new(map { $_->$method(@args) } @$self); | |
+ return $self->new( | |
+ map { ref $_ eq 'HASH' ? $_->{$method} : $_->$method(@args) } @$self); | |
} | |
sub reduce { | |
@@ -262,13 +263,15 @@ Construct a new array-based L<Mojo::Collection> object. | |
=head2 pluck | |
+ my $new = $collection->pluck($key); | |
my $new = $collection->pluck($method); | |
my $new = $collection->pluck($method, @args); | |
-Call method on each element in collection and create a new collection from the | |
-results. | |
+Extract value from hash reference or call method on each element in collection | |
+and create a new collection from the results. | |
# Equal to but more convenient than | |
+ my $new = $collection->map(sub { $_->{$key} }); | |
my $new = $collection->map(sub { $_->$method(@args) }); | |
=head2 reduce | |
diff --git a/t/mojo/collection.t b/t/mojo/collection.t | |
index 066e292..dfe67fd 100644 | |
--- a/t/mojo/collection.t | |
+++ b/t/mojo/collection.t | |
@@ -151,6 +151,8 @@ $collection = c(b('one'), b('two'), b('three')); | |
is $collection->camelize, "One\nTwo\nThree", 'right result'; | |
is $collection->url_escape('^netwhr')->reverse, "%54hree\n%54w%6F\n%4Fne", | |
'right result'; | |
+is c({foo => 'bar'}, {foo => 'baz'})->pluck('foo')->join, 'barbaz', | |
+ 'right result'; | |
# uniq | |
$collection = c(1, 2, 3, 2, 3, 4, 5, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment