Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
deprecated Mojo::Collection::pluck in favor of Mojo::Collection::map
  • Loading branch information
kraih committed Nov 2, 2014
1 parent 3a29749 commit 2551b57
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 70 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,7 +1,9 @@

5.57 2014-11-02
- Deprecated stringify support in Mojo::Collection.
- Deprecated Mojo::Collection::pluck in favor of Mojo::Collection::map.
- Deprecated Mojo::DOM::val.
- Improved map method in Mojo::Collection to be able to call methods.

5.56 2014-10-29
- Deprecated Mojo::Collection::AUTOLOAD in favor of Mojo::Collection::pluck.
Expand Down
34 changes: 14 additions & 20 deletions lib/Mojo/Collection.pm
Expand Up @@ -22,12 +22,12 @@ our @EXPORT_OK = ('c');
# DEPRECATED in Tiger Face!
sub AUTOLOAD {
deprecated 'Mojo::Collection::AUTOLOAD is DEPRECATED in favor of'
. ' Mojo::Collection::pluck';
. ' Mojo::Collection::map';
my $self = shift;
my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/;
croak "Undefined subroutine &${package}::$method called"
unless blessed $self && $self->isa(__PACKAGE__);
return $self->pluck($method, @_);
return $self->map($method, @_);
}

# DEPRECATED in Tiger Face!
Expand Down Expand Up @@ -69,8 +69,8 @@ sub join {
sub last { shift->[-1] }

sub map {
my ($self, $cb) = @_;
return $self->new(map { $_->$cb } @$self);
my ($self, $cb) = (shift, shift);
return $self->new(map { $_->$cb(@_) } @$self);
}

sub new {
Expand All @@ -79,6 +79,8 @@ sub new {
}

sub pluck {
deprecated
'Mojo::Collection::pluck is DEPRECATED in favor of Mojo::Collection::map';
my ($self, $key) = (shift, shift);
return $self->new(map { ref eq 'HASH' ? $_->{$key} : $_->$key(@_) } @$self);
}
Expand Down Expand Up @@ -255,10 +257,15 @@ Return the last element in collection.
=head2 map
my $new = $collection->map(sub {...});
my $new = $collection->map($method);
my $new = $collection->map($method, @args);
Evaluate callback for each element in collection and create a new collection
from the results. The element will be the first argument passed to the
callback and is also available as C<$_>.
Evaluate callback for, or call method on, each element in collection and
create a new collection from the results. The element will be the first
argument passed to the callback and is also available as C<$_>.
# Longer version
my $new = $collection->map(sub { $_->$method(@args) });
# Append the word "mojo" to all values
my $mojoified = $collection->map(sub { $_ . 'mojo' });
Expand All @@ -269,19 +276,6 @@ callback and is also available as C<$_>.
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);
Extract hash reference value from, or call method on, each element in
collection and create a new collection from the results.
# Longer version
my $new = $collection->map(sub { $_->{$key} });
my $new = $collection->map(sub { $_->$method(@args) });
=head2 reduce
my $result = $collection->reduce(sub {...});
Expand Down
20 changes: 10 additions & 10 deletions lib/Mojo/DOM.pm
Expand Up @@ -191,7 +191,7 @@ sub val {
if $type eq 'option';

# "select"
return $self->find('option[selected]')->pluck('val')->flatten
return $self->find('option[selected]')->map('val')->flatten
if $type eq 'select';

# "textarea"
Expand Down Expand Up @@ -422,8 +422,8 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
# Find
say $dom->at('#b')->text;
say $dom->find('p')->pluck('text')->join("\n");
say $dom->find('[id]')->pluck(attr => 'id')->join("\n");
say $dom->find('p')->map('text')->join("\n");
say $dom->find('[id]')->map(attr => 'id')->join("\n");
# Iterate
$dom->find('p[id]')->reverse->each(sub { say $_->{id} });
Expand All @@ -435,7 +435,7 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
# Modify
$dom->find('div p')->last->append('<p id="c">456</p>');
$dom->find(':not(p)')->pluck('strip');
$dom->find(':not(p)')->map('strip');
# Render
say "$dom";
Expand Down Expand Up @@ -481,7 +481,7 @@ L<Mojo::DOM> objects.
# "<p><b>123</b></p>"
$dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')->all_contents
->grep(sub { $_->node eq 'comment' })->pluck('remove')->first;
->grep(sub { $_->node eq 'comment' })->map('remove')->first;
=head2 all_text
Expand All @@ -507,7 +507,7 @@ L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# List types of ancestor elements
say $dom->ancestors->pluck('type')->join("\n");
say $dom->ancestors->map('type')->join("\n");
=head2 append
Expand Down Expand Up @@ -561,7 +561,7 @@ from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
This element's attributes.
# List id attributes
say $dom->find('*')->pluck(attr => 'id')->compact->join("\n");
say $dom->find('*')->map(attr => 'id')->compact->join("\n");
=head2 children
Expand Down Expand Up @@ -627,7 +627,7 @@ All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
my $id = $dom->find('div')->[23]{id};
# Extract information from multiple elements
my @headers = $dom->find('h1, h2, h3')->pluck('text')->each;
my @headers = $dom->find('h1, h2, h3')->map('text')->each;
# Count all the different tags
my $hash = $dom->find('*')->reduce(sub { $a->{$b->type}++; $a }, {});
Expand Down Expand Up @@ -800,7 +800,7 @@ L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# List types of sibling elements
say $dom->siblings->pluck('type')->join("\n");
say $dom->siblings->map('type')->join("\n");
=head2 strip
Expand Down Expand Up @@ -856,7 +856,7 @@ carefully since it is very dynamic.
This element's type.
# List types of child elements
say $dom->children->pluck('type')->join("\n");
say $dom->children->map('type')->join("\n");
=head2 wrap
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -488,11 +488,11 @@ whole message body needs to be loaded into memory to parse it, so you have to
make sure it is not excessively large, there's a 10MB limit by default.
# Perform "find" right away
say $msg->dom('h1, h2, h3')->pluck('text');
say $msg->dom('h1, h2, h3')->map('text')->join("\n");
# Use everything else Mojo::DOM has to offer
say $msg->dom->at('title')->text;
say $msg->dom->at('body')->children->pluck('type')->uniq->join("\n");
say $msg->dom->at('body')->children->map('type')->uniq->join("\n");
=head2 error
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -382,7 +382,7 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
# Scrape the latest headlines from a news site with CSS selectors
say $ua->get('blogs.perl.org')
->res->dom->find('h2 > a')->pluck('text')->join("\n");
->res->dom->find('h2 > a')->map('text')->join("\n");
# Search DuckDuckGo anonymously through Tor
$ua->proxy->http('socks://127.0.0.1:9050');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -874,7 +874,7 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
my $c = shift;
my $url = $c->param('url') || 'http://mojolicio.us';
my $dom = $c->ua->get($url)->res->dom;
$c->render(json => [$dom->find('h1, h2, h3')->pluck('text')->each]);
$c->render(json => [$dom->find('h1, h2, h3')->map('text')->each]);
};
# Non-blocking
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -42,7 +42,7 @@ sub _html {
my $perldoc = $c->url_for('/perldoc/');
$_->{href} =~ s!^https://metacpan\.org/pod/!$perldoc!
and $_->{href} =~ s!::!/!gi
for $dom->find('a[href]')->pluck('attr')->each;
for $dom->find('a[href]')->map('attr')->each;

# Rewrite code blocks for syntax highlighting and correct indentation
for my $e ($dom->find('pre > code')->each) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ojo.pm
Expand Up @@ -125,7 +125,7 @@ resulting L<Mojo::Message::Response> object.
Perform C<GET> request with L<Mojo::UserAgent/"get"> and return resulting
L<Mojo::Message::Response> object.
$ perl -Mojo -E 'say g("mojolicio.us")->dom("h1, h2, h3")->pluck("text")'
$ perl -Mojo -E 'say g("mojolicio.us")->dom("h1")->map("text")->join("\n")'
=head2 h
Expand Down
14 changes: 5 additions & 9 deletions t/mojo/collection.t
Expand Up @@ -82,6 +82,11 @@ is $collection->map(sub { $_ + 1 })->join(''), '234', 'right result';
is_deeply [@$collection], [1, 2, 3], 'right elements';
is $collection->map(sub { shift() + 2 })->join(''), '345', 'right result';
is_deeply [@$collection], [1, 2, 3], 'right elements';
$collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
is $collection->map('reverse')->map(join => "\n")->join("\n"),
"3\n2\n1\n6\n5\n4\n9\n8\n7", 'right result';
is $collection->map(join => '-')->join("\n"), "1-2-3\n4-5-6\n7-8-9",
'right result';

# reverse
$collection = c(3, 2, 1);
Expand Down Expand Up @@ -141,15 +146,6 @@ is_deeply [$collection->slice(1, 2, 3)->each], [2, 3, 4], 'right result';
is_deeply [$collection->slice(6, 1, 4)->each], [7, 2, 5], 'right result';
is_deeply [$collection->slice(6 .. 9)->each], [7, 10, 9, 8], 'right result';

# pluck
is c({foo => 'bar'}, {foo => 'baz'})->pluck('foo')->join, 'barbaz',
'right result';
$collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
is $collection->pluck('reverse')->pluck(join => "\n")->join("\n"),
"3\n2\n1\n6\n5\n4\n9\n8\n7", 'right result';
is $collection->pluck(join => '-')->join("\n"), "1-2-3\n4-5-6\n7-8-9",
'right result';

# uniq
$collection = c(1, 2, 3, 2, 3, 4, 5, 4);
is_deeply [$collection->uniq->each], [1, 2, 3, 4, 5], 'right result';
Expand Down
32 changes: 16 additions & 16 deletions t/mojo/dom.t
Expand Up @@ -8,7 +8,7 @@ my $dom = Mojo::DOM->new->parse(
'<div><div FOO="0" id="a">A</div><div id="b">B</div></div>');
is $dom->at('#b')->text, 'B', 'right text';
my @div;
push @div, $dom->find('div[id]')->pluck('text')->each;
push @div, $dom->find('div[id]')->map('text')->each;
is_deeply \@div, [qw(A B)], 'found all div elements with id';
@div = ();
$dom->find('div[id]')->each(sub { push @div, $_->text });
Expand All @@ -20,7 +20,7 @@ is "$dom", '<div><div foo="0" id="a">A</div><div id="b">B</div></div>',

# Tap into method chain
$dom = Mojo::DOM->new->parse('<div id="a">A</div><div id="b">B</div>');
is_deeply [$dom->find('[id]')->pluck(attr => 'id')->each], [qw(a b)],
is_deeply [$dom->find('[id]')->map(attr => 'id')->each], [qw(a b)],
'right result';
is $dom->tap(sub { $_->at('#b')->remove }), '<div id="a">A</div>',
'right result';
Expand Down Expand Up @@ -143,7 +143,7 @@ is $dom->next, undef, 'no siblings';
is $dom->previous, undef, 'no siblings';
is $dom->at('foo > a')->next, undef, 'no next sibling';
is $dom->at('foo > simple')->previous, undef, 'no previous sibling';
is_deeply [$dom->at('simple')->ancestors->pluck('type')->each], ['foo'],
is_deeply [$dom->at('simple')->ancestors->map('type')->each], ['foo'],
'right results';
ok !$dom->at('simple')->ancestors->first->xml, 'XML mode not active';

Expand Down Expand Up @@ -186,8 +186,8 @@ is $dom->contents->[2]->node, 'pi', 'right node';
is $dom->contents->[2]->content, 'after', 'right content';
is $dom->contents->first->content(' again')->content, ' again',
'right content';
is $dom->contents->grep(sub { $_->node eq 'pi' })->pluck('remove')
->first->node, 'root', 'right node';
is $dom->contents->grep(sub { $_->node eq 'pi' })->map('remove')->first->node,
'root', 'right node';
is "$dom", '<!DOCTYPE again><p><![CDATA[123]]><!-- 456 --></p>',
'right result';

Expand All @@ -214,7 +214,7 @@ is $dom->at('script')->contents->first->wrap('<i>:)</i>')->root,
'<script><i>:)a</i><b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('i')->contents->first->wrap_content('<b></b>')->root,
'<script><i><b>:)</b>a</i><b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('b')->contents->first->ancestors->pluck('type')->join(','),
is $dom->at('b')->contents->first->ancestors->map('type')->join(','),
'b,i,script', 'right result';
is $dom->at('b')->contents->first->append_content('g')->content, ':)g',
'right content';
Expand Down Expand Up @@ -284,7 +284,7 @@ $dom->find('p')->each(sub { push @p, $_->attr('id') });
is_deeply \@p, [qw(foo bar)], 'found all p elements';
my $ids = [qw(container header logo buttons buttons content)];
is_deeply \@div, $ids, 'found all div elements';
is_deeply [$dom->at('p')->ancestors->pluck('type')->each],
is_deeply [$dom->at('p')->ancestors->map('type')->each],
[qw(div div div body html)], 'right results';
is_deeply [$dom->at('html')->ancestors->each], [], 'no results';
is_deeply [$dom->ancestors->each], [], 'no results';
Expand All @@ -294,8 +294,8 @@ ok $dom->at('form')->siblings->[1]->match('#content'), 'right sibling';
is $dom->at('form')->siblings('#content')->first->text, 'More stuff',
'right text';
is_deeply [$dom->at('form')->siblings('#nothing')->each], [], 'no results';
is_deeply [$dom->at('#header')->siblings->pluck('type')->each],
[qw(form div)], 'right results';
is_deeply [$dom->at('#header')->siblings->map('type')->each], [qw(form div)],
'right results';

# Script tag
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down Expand Up @@ -437,7 +437,7 @@ is $dom->replace(''), '', 'no result';
is "$dom", '', 'no result';
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
$dom->find('p')->pluck(replace => '');
$dom->find('p')->map(replace => '');
is "$dom", '<div>foobar</div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>♥</div>');
$dom->at('div')->content('');
Expand All @@ -451,14 +451,14 @@ is $dom->replace('<b>whatever</b>')->root, '<b>whatever</b>', 'right result';
is $dom->to_string, '<b>whatever</b>', 'right result';
$dom->at('b')->prepend('<p>foo</p>')->append('<p>bar</p>');
is "$dom", '<p>foo</p><b>whatever</b><p>bar</p>', 'right result';
is $dom->find('p')->pluck('remove')->first->root->at('b')->text, 'whatever',
is $dom->find('p')->map('remove')->first->root->at('b')->text, 'whatever',
'right result';
is "$dom", '<b>whatever</b>', 'right result';
is $dom->at('b')->strip, 'whatever', 'right result';
is $dom->strip, 'whatever', 'right result';
is $dom->remove, '', 'right result';
$dom->replace('A<div>B<p>C<b>D<i><u>E</u></i>F</b>G</p><div>H</div></div>I');
is $dom->find(':not(div):not(i):not(u)')->pluck('strip')->first->root,
is $dom->find(':not(div):not(i):not(u)')->map('strip')->first->root,
'A<div>BCD<i><u>E</u></i>FG<div>H</div></div>I', 'right result';
is $dom->at('i')->to_string, '<i><u>E</u></i>', 'right result';

Expand Down Expand Up @@ -537,7 +537,7 @@ $dom = Mojo::DOM->new->parse(<<EOF);
EOF
ok $dom->xml, 'XML mode detected';
is $dom->find('rss')->[0]->attr('version'), '2.0', 'right version';
is_deeply [$dom->at('title')->ancestors->pluck('type')->each],
is_deeply [$dom->at('title')->ancestors->map('type')->each],
[qw(channel rss)], 'right results';
is $dom->at('extension')->attr('foo:id'), 'works', 'right id';
like $dom->at('#works')->text, qr/\[awesome\]\]/, 'right text';
Expand Down Expand Up @@ -1427,7 +1427,7 @@ is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar',
is $dom->at('table > thead > tr > th')->text, 'A', 'right text';
is $dom->find('table > thead > tr > th')->[1]->text, 'D', 'right text';
is $dom->at('table > tbody > tr > td')->text, 'B', 'right text';
is $dom->find('table > tbody > tr > td')->pluck('text')->join("\n"), "B\nE",
is $dom->find('table > tbody > tr > td')->map('text')->join("\n"), "B\nE",
'right text';

# Optional "colgroup", "tbody", "tr", "th" and "td" tags
Expand Down Expand Up @@ -1508,8 +1508,8 @@ is $dom->find('tbody > tr > .gamma > a')->[0]->text, 'Gamma', 'right text';
is $dom->find('tbody > tr > .alpha')->[1]->text, 'Alpha Two', 'right text';
is $dom->find('tbody > tr > .gamma > a')->[1]->text, 'Gamma Two', 'right text';
my @siblings
= $dom->find('tr > td:nth-child(1)')->pluck(siblings => ':nth-child(even)')
->flatten->pluck('all_text')->each;
= $dom->find('tr > td:nth-child(1)')->map(siblings => ':nth-child(even)')
->flatten->map('all_text')->each;
is_deeply \@siblings, ['Beta', 'Delta', 'Beta Two', 'Delta Two'],
'right results';

Expand Down
14 changes: 7 additions & 7 deletions t/mojo/response.t
Expand Up @@ -1038,18 +1038,18 @@ is $res->version, '1.1', 'right version';
is $res->dom->at('p')->text, 'foo', 'right value';
is $res->dom->at('p > a')->text, 'bar', 'right value';
is $res->dom('p')->first->text, 'foo', 'right value';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(bar baz)],
is_deeply [$res->dom('p > a')->map('text')->each], [qw(bar baz)],
'right values';
my @text = $res->dom('a')->pluck(content => 'yada')->first->root->find('p > a')
->pluck('text')->each;
my @text = $res->dom('a')->map(content => 'yada')->first->root->find('p > a')
->map('text')->each;
is_deeply \@text, [qw(yada yada)], 'right values';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(yada yada)],
is_deeply [$res->dom('p > a')->map('text')->each], [qw(yada yada)],
'right values';
@text
= $res->dom->find('a')->pluck(content => 'test')->first->root->find('p > a')
->pluck('text')->each;
= $res->dom->find('a')->map(content => 'test')->first->root->find('p > a')
->map('text')->each;
is_deeply \@text, [qw(test test)], 'right values';
is_deeply [$res->dom->find('p > a')->pluck('text')->each], [qw(test test)],
is_deeply [$res->dom->find('p > a')->map('text')->each], [qw(test test)],
'right values';

# Build DOM from response with charset
Expand Down

0 comments on commit 2551b57

Please sign in to comment.