fixed add/before pre/app pend content stuff, added docs and test, also docs and test...
[catagits/HTML-Zoom.git] / lib / HTML / Zoom.pm
index 3dee0b7..ee24979 100644 (file)
@@ -6,6 +6,7 @@ use HTML::Zoom::ZConfig;
 use HTML::Zoom::ReadFH;
 use HTML::Zoom::Transform;
 use HTML::Zoom::TransformBuilder;
+use Scalar::Util ();
 
 our $VERSION = '0.009004';
 
@@ -149,19 +150,11 @@ 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 ($self, $selector, @args) = @_;
   my $meth = our $AUTOLOAD;
   $meth =~ s/.*:://;
-  while( my($selector, $args) = each %selection_args) {
-    $self = $self->select($selector)->$meth($args);
-  }
-  return $self
+  return $self = $self->select($selector)->$meth(@args);
 }
 
 ## mst: I've been thinking
@@ -172,20 +165,29 @@ sub AUTOLOAD {
 =head2 fill
 
   @args = ($selector => $text||\$text) ## $zoom->select($selector)->replace_content($text)
-  @args =>($selector => \@arrayref||$coderef) ## $zoom->select($selector)->repeat_content(\@arrayref)
+  @args = ($selector => \@arrayref||$coderef) ## $zoom->select($selector)->repeat_content(\@arrayref)
+  @args = [qw/a b c/] => {}, $object
 
 =cut
 
 sub fill {
   my $self = shift;
-  my %selection_args = @_;
-  while( my($selector, $args) = each %selection_args) {
-    my $type = ref($args) ? ref($args) : 'SCALAR';
+  while(@_) {
+    my $selector = shift;
+    my $args = shift;
+    my $type = Scalar::Util::reftype($args) ? Scalar::Util::reftype($args) : 'SCALAR';
     if($type eq 'ARRAY' || $type eq 'CODE') {
       $self = $self->select($selector)->repeat_content($args);
     } elsif($type eq 'SCALAR') {
-        warn "$selector, $args";
       $self = $self->select($selector)->replace_content($args);
+    } elsif($type eq 'HASH') {
+      die "Selector must be ArrayRef"
+        unless ref($selector) eq 'ARRAY';
+      foreach my $attr (@$selector) {
+        my $val = Scalar::Util::blessed($args) ? $args->$attr : $args->{$attr};
+        next unless defined($val);
+        $self = $self->select(".$attr")->replace_content($val);
+      }
     } else {
       die "I don't know what to do with args of type $type";
     }
@@ -815,6 +817,20 @@ together; the intermediary object isn't designed or expected to stick around.
 Re-runs the previous select to allow you to chain actions together on the
 same selector.
 
+=head1 AUTOLOAD METHODS
+
+L<HTML::Zoom> AUTOLOADS methods against L</select> so that you can reduce a
+certain amount of boilerplate typing.  This allows you to replace:
+
+  $z->select('div')->replace_content("Hello World");
+  
+With:
+
+  $z->replace_content(div => "Hello World");
+  
+Besides saving a few keys per invocations, you may feel this looks neater
+in your code and increases understanding.
+
 =head1 AUTHOR
 
 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>