From: Simon Elliott Date: Thu, 19 May 2011 16:23:44 +0000 (+0100) Subject: Support for hashref in AUTOLOADed methods i.e X-Git-Tag: allocate~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=6af04df5d563b2044ff7d65c88f977557ec17e48 Support for hashref in AUTOLOADed methods i.e $z->repeat_content({ div => ['foo'], span => ['bar'], }) --- diff --git a/lib/HTML/Zoom.pm b/lib/HTML/Zoom.pm index baeec15..1cb15de 100644 --- a/lib/HTML/Zoom.pm +++ b/lib/HTML/Zoom.pm @@ -155,7 +155,19 @@ sub AUTOLOAD { my $sel = $self->select($selector); my $meth = our $AUTOLOAD; $meth =~ s/.*:://; - if(my $cr = $sel->_zconfig->filter_builder->can($meth)) { + if (ref($selector) eq 'HASH') { + my $ret = $self; + $ret = $ret->_do($_, $meth, @{$selector->{$_}}) for keys %$selector; + $ret; + } else { + $self->_do($selector, $meth, @args); + } +} + +sub _do { + my ($self, $selector, $meth, @args) = @_; + my $sel = $self->select($selector); + if( my $cr = $sel->_zconfig->filter_builder->can($meth)) { return $sel->$meth(@args); } else { die "We can't do $meth on ->select('$selector')"; @@ -754,7 +766,7 @@ Oliver Charles Jakub Nareski -Simon Elliot +Simon Elliott Joe Highton diff --git a/t/dwim-autoload.t b/t/dwim-autoload.t index 54de2fd..6441d1f 100644 --- a/t/dwim-autoload.t +++ b/t/dwim-autoload.t @@ -322,4 +322,15 @@ sub code_stream (&) { 'Got correct from repeat_content'; } +{ + ok my $dwim = HTML::Zoom + ->from_html(q[]) + ->replace_content({ + 'li.foo' => ['foo'], + 'li.bar' => ['bar'], + })->to_html; + is $dwim, '', + 'Multiple selectors from hashref'; +} + done_testing;