"$zoom->$whatever($selector => @args)" ==> $zoom->select($selector)->$whatever(...
John Napiorkowski [Mon, 28 Mar 2011 22:08:25 +0000 (18:08 -0400)]
lib/HTML/Zoom.pm
t/dwim-autoload.t [new file with mode: 0644]

index 4f1c599..14401de 100644 (file)
@@ -149,6 +149,20 @@ sub then {
   $self->select($self->{transforms}->[-1]->selector);
 }
 
+## mst: well I'm thinking if basically
+## mst: $zoom->$whatever($selector => @args)
+## mst: becomes $zoom->select($selector)->$whatever(@args)
+
+sub AUTOLOAD {
+  my $self = shift;
+  my %selection_args = @_;
+  my $meth = our $AUTOLOAD;
+  $meth =~ s/.*:://;
+  while( my($selector, $args) = each %selection_args) {
+    $self = $self->select($selector)->$meth($args);
+  }
+  return $self
+}
 1;
 
 =head1 NAME
diff --git a/t/dwim-autoload.t b/t/dwim-autoload.t
new file mode 100644 (file)
index 0000000..5a3a314
--- /dev/null
@@ -0,0 +1,54 @@
+use strict;
+use warnings FATAL => 'all';
+
+use HTML::Zoom;
+use Test::More;
+
+ok my $zoom = HTML::Zoom->from_html(<<HTML);
+<html>
+  <head>
+    <title>Hi!</title>
+  </head>
+  <body id="content-area">
+    <h1>Test</h1>
+    <div>
+      <p class="first-para">Some Stuff</p>
+      <p class="body-para">More Stuff</p>
+      <p class="body-para">Even More Stuff</p>
+      <ol>
+        <li class="first-item odd">First</li>
+        <li class="body-items even">Stuff A</li>
+        <li class="body-items odd">Stuff B</li>
+        <li class="body-items even">Stuff C</li>
+        <li class="last-item odd">Last</li>
+      </ol>
+      <p class="body-para">Even More stuff</p>
+      <p class="last-para">Some Stuff</p>
+    </div>
+    <p id="footer">Copyright 2222</p>
+  </body>
+</html>
+HTML
+
+## mst: well I'm thinking if basically
+## mst: $zoom->$whatever($selector => @args)
+## mst: becomes $zoom->select($selector)->$whatever(@args)
+
+ok ! eval { $zoom->ressplace_content('.first-param' => 'First!'); 1},
+  'Properly die on bad method';
+
+ok my $html = $zoom
+  ->replace_content('.first-para' => 'First!')
+  ->replace_content('.last-para' => 'Last!')
+  ->prepend_content(
+    '.first-item' => [{type=>'TEXT', raw=>'FIRST: '}],
+    '.last-item' => [{type=>'TEXT', raw=>'LAST: '}],
+  )
+  ->to_html;
+
+warn $html;
+
+#warn $zoom->select('.last-item')->prepend_content([{type=>'TEXT', raw=>'ddd'}])->to_html;
+#warn $zoom->select('.last-item')->prepend_content(\'ddd')->to_html;
+
+done_testing;