]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blobdiff - lib/LaTeX/TikZ/Functor/Rule.pm
Don't undef the $rule in LT::Functor::Rule::insert
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Functor / Rule.pm
index efaff7b1cc73078b012ff9b7499ec87feacad17f..df5b553eb5a7256dd7e253f029c75f16d42a230f 100644 (file)
@@ -9,15 +9,15 @@ LaTeX::TikZ::Functor::Rule - An object that specifies how functors should handle
 
 =head1 VERSION
 
-Version 0.01
+Version 0.02
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 DESCRIPTION
 
-A rule specifies how functors should handle a certain kind of set or mod.
+A rule specifies how functors (L<LaTeX::TikZ::Functor> objects) should handle a certain kind of set or mod.
 A functor is basically an ordered collection of rules.
 
 =cut
@@ -32,6 +32,9 @@ use Any::Moose 'Util::TypeConstraints';
 
 =head2 C<target>
 
+A class or role name against which set or mod candidates will be matched.
+It must consume either L<LaTeX::TikZ::Set> or L<LaTeX::TikZ::Mod>, directly or through inheritance.
+
 =cut
 
 has 'target' => (
@@ -42,6 +45,9 @@ has 'target' => (
 
 =head2 C<handler>
 
+The code reference executed when the rule handles a given set or mod object.
+It is called with the L<LaTeX::TikZ::Functor> object as its first argument, the set/mod object as its second, and then the arguments passed to the functor itself.
+
 =cut
 
 has 'handler' => (
@@ -52,6 +58,8 @@ has 'handler' => (
 
 =head2 C<is_role>
 
+True if and only if the target is a role.
+
 =cut
 
 has 'is_role' => (
@@ -62,6 +70,8 @@ has 'is_role' => (
 
 =head2 C<is_set>
 
+True when the target does the L<LaTeX::TikZ::Set> role, and false when it does L<LaTeX::TikZ::Mod>.
+
 =cut
 
 has 'is_set' => (
@@ -77,6 +87,8 @@ my $ltfrl_tc = subtype 'LaTeX::TikZ::Functor::RuleList'
 
 =head2 C<< new target => $target, handler => $handler >>
 
+Constructs a new rule object with target C<$target> and handler C<$handler>.
+
 =cut
 
 around 'BUILDARGS' => sub {
@@ -132,26 +144,26 @@ sub insert {
  my $is_role = $rule->is_role;
 
  if ($replace) {
-  my @remove;
+  my (@remove, $replaced);
 
   for my $i (0 .. $#$list) {
    my $old_target = $list->[$i]->target;
    if ($rule->handles($old_target)) {
-    if (defined $rule) {
-     splice @$list, $i, 1, $rule;
-     $rule = undef;
-    } else {
+    if ($replaced) {
      push @remove, $i;
+    } else {
+     splice @$list, $i, 1, $rule;
+     $replaced = 1;
     }
    }
   }
 
-  my $shift;
+  my $shift = 0;
   for (@remove) {
    splice @$list, $_ - $shift, 1;
    ++$shift;
   }
-  return 1 unless defined $rule;
+  return 1 if $replaced;
 
  } else { # Replace only an existent rule