Support for hashref in AUTOLOADed methods i.e
Simon Elliott [Thu, 19 May 2011 16:23:44 +0000 (17:23 +0100)]
$z->repeat_content({
  div => ['foo'],
  span => ['bar'],
})

lib/HTML/Zoom.pm
t/dwim-autoload.t

index baeec15..1cb15de 100644 (file)
@@ -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
 
index 54de2fd..6441d1f 100644 (file)
@@ -322,4 +322,15 @@ sub code_stream (&) {
     'Got correct from repeat_content';
 }
 
+{
+  ok my $dwim = HTML::Zoom
+    ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
+    ->replace_content({
+      'li.foo' => ['foo'],
+      'li.bar' => ['bar'],
+    })->to_html;
+  is $dwim, '<ul><li class="foo">foo</li><li class="bar">bar</li></ul>',
+    'Multiple selectors from hashref';
+}
+
 done_testing;