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';
22 use LaTeX::TikZ::Interface;
24 use LaTeX::TikZ::Tools;
26 use Any::Moose 'Util' => [ 'does_role' ];
28 my $lts_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set');
30 my @default_set_rules;
31 my @default_mod_rules;
33 my ($validate_rule, $insert_rule);
35 $validate_rule = Sub::Name::subname('validate_rule' => sub {
36 my ($target, $handler) = @_;
38 unless (defined $target and ref $target eq ''
39 and $target =~ /[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z][A-Za-z0-9_]*)*/) {
40 Carp::confess("Invalid target $target");
43 (my $pm = $target) =~ s{::}{/}g;
48 if (does_role($target, 'LaTeX::TikZ::Set')) {
50 } elsif (does_role($target, 'LaTeX::TikZ::Mod')) {
53 Carp::confess("Target $target is neither a set nor a mod");
56 Carp::confess("Invalid handler for target $target")
57 unless ref $handler eq 'CODE';
59 return [ $target, $handler, $is_set ];
62 $insert_rule = Sub::Name::subname('insert_rule' => sub {
63 my ($rule, $list) = @_;
65 my $spec = $rule->[0];
66 for my $i (0 .. $#$list) {
67 my $old_spec = $list->[$i]->[0];
68 if ($old_spec->isa($spec) or does_role($old_spec, $spec)) {
69 splice @$list, $i, 1, $rule;
82 my $rule = $validate_rule->(@_);
84 $insert_rule->($rule, $rule->[2] ? \@default_set_rules : \@default_mod_rules);
88 my ($class, %args) = @_;
90 my @set_rules = @default_set_rules;
91 my @mod_rules = @default_mod_rules;
93 my @user_rules = @{$args{rules} || []};
95 my ($target, $handler) = splice @user_rules, 0, 2;
97 my $rule = $validate_rule->($target, $handler);
99 $insert_rule->($rule, $rule->[2] ? \@set_rules : \@mod_rules);
102 my %dispatch = map { $_->[0] => $_ } @set_rules, @mod_rules;
109 $lts_tc->assert_valid($set);
111 my $rule = $dispatch{ref($set)};
113 ($set->isa($_->[0]) or $set->does($_->[0])) and $rule = $_ for @set_rules;
114 $rule = [ undef, sub { $_[1] } ] unless $rule;
116 my $new_set = $rule->[1]->($self, $set, @_);
117 my $is_new = $new_set ne $set;
121 for my $mod ($set->mods) {
122 my $rule = $dispatch{ref($mod)};
124 ($mod->isa($_->[0]) or $mod->does($_->[0])) and $rule = $_ for @mod_rules;
126 push @new_mods, $mod;
130 push @new_mods, $rule->[1]->($self, $mod, @_);
133 $new_set->mod(@new_mods) if $is_new;
139 LaTeX::TikZ::Interface->register(
143 __PACKAGE__->new(rules => \@_);
149 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
151 You can contact me by mail or on C<irc.perl.org> (vincent).
155 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>.
156 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
160 You can find documentation for this module with the perldoc command.
164 =head1 COPYRIGHT & LICENSE
166 Copyright 2010 Vincent Pit, all rights reserved.
168 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
172 1; # End of LaTeX::TikZ::Functor