1 package LaTeX::TikZ::Functor;
8 LaTeX::TikZ::Functor - Build functor methods that recursively visit nodes of a LaTeX::TikZ::Set tree.
16 our $VERSION = '0.01';
20 A functor takes a L<LaTeX::TikZ::Set> tree and clones it according to certain rules.
28 use LaTeX::TikZ::Interface;
30 use LaTeX::TikZ::Tools;
32 use Any::Moose 'Util' => [ 'does_role' ];
34 my $lts_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set');
36 my @default_set_rules;
37 my @default_mod_rules;
39 my ($validate_rule, $insert_rule);
41 $validate_rule = Sub::Name::subname('validate_rule' => sub {
42 my ($target, $handler) = @_;
44 unless (defined $target and ref $target eq ''
45 and $target =~ /[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z][A-Za-z0-9_]*)*/) {
46 Carp::confess("Invalid target $target");
49 (my $pm = $target) =~ s{::}{/}g;
54 if (does_role($target, 'LaTeX::TikZ::Set')) {
56 } elsif (does_role($target, 'LaTeX::TikZ::Mod')) {
59 Carp::confess("Target $target is neither a set nor a mod");
62 Carp::confess("Invalid handler for target $target")
63 unless ref $handler eq 'CODE';
65 return [ $target, $handler, $is_set ];
68 $insert_rule = Sub::Name::subname('insert_rule' => sub {
69 my ($rule, $list) = @_;
71 my $spec = $rule->[0];
72 for my $i (0 .. $#$list) {
73 my $old_spec = $list->[$i]->[0];
74 if ($old_spec->isa($spec) or does_role($old_spec, $spec)) {
75 splice @$list, $i, 1, $rule;
87 =head2 C<default_rule>
94 my $rule = $validate_rule->(@_);
96 $insert_rule->($rule, $rule->[2] ? \@default_set_rules : \@default_mod_rules);
99 =head2 C<< new rules => [ $class_name => sub { ... }, ... ] >>
104 my ($class, %args) = @_;
106 my @set_rules = @default_set_rules;
107 my @mod_rules = @default_mod_rules;
109 my @user_rules = @{$args{rules} || []};
110 while (@user_rules) {
111 my ($target, $handler) = splice @user_rules, 0, 2;
113 my $rule = $validate_rule->($target, $handler);
115 $insert_rule->($rule, $rule->[2] ? \@set_rules : \@mod_rules);
118 my %dispatch = map { $_->[0] => $_ } @set_rules, @mod_rules;
125 $lts_tc->assert_valid($set);
127 my $rule = $dispatch{ref($set)};
129 ($set->isa($_->[0]) or $set->does($_->[0])) and $rule = $_ for @set_rules;
130 $rule = [ undef, sub { $_[1] } ] unless $rule;
132 my $new_set = $rule->[1]->($self, $set, @_);
133 my $is_new = $new_set ne $set;
137 for my $mod ($set->mods) {
138 my $rule = $dispatch{ref($mod)};
140 ($mod->isa($_->[0]) or $mod->does($_->[0])) and $rule = $_ for @mod_rules;
142 push @new_mods, $mod;
146 push @new_mods, $rule->[1]->($self, $mod, @_);
149 $new_set->mod(@new_mods) if $is_new;
155 LaTeX::TikZ::Interface->register(
159 __PACKAGE__->new(rules => \@_);
165 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
167 You can contact me by mail or on C<irc.perl.org> (vincent).
171 Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
172 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
176 You can find documentation for this module with the perldoc command.
180 =head1 COPYRIGHT & LICENSE
182 Copyright 2010 Vincent Pit, all rights reserved.
184 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
188 1; # End of LaTeX::TikZ::Functor