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::Tools;
24 use Any::Moose 'Util' => [ 'does_role' ];
26 my $lts_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set');
28 my @default_set_rules;
29 my @default_mod_rules;
31 my ($validate_rule, $insert_rule);
33 $validate_rule = Sub::Name::subname('validate_rule' => sub {
34 my ($target, $handler) = @_;
36 unless (defined $target and ref $target eq ''
37 and $target =~ /[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z][A-Za-z0-9_]*)*/) {
38 Carp::confess("Invalid target $target");
41 (my $pm = $target) =~ s{::}{/}g;
46 if (does_role($target, 'LaTeX::TikZ::Set')) {
48 } elsif (does_role($target, 'LaTeX::TikZ::Mod')) {
51 Carp::confess("Target $target is neither a set nor a mod");
54 Carp::confess("Invalid handler for target $target")
55 unless ref $handler eq 'CODE';
57 return [ $target, $handler, $is_set ];
60 $insert_rule = Sub::Name::subname('insert_rule' => sub {
61 my ($rule, $list) = @_;
63 my $spec = $rule->[0];
64 for my $i (0 .. $#$list) {
65 my $old_spec = $list->[$i]->[0];
66 if ($old_spec->isa($spec) or does_role($old_spec, $spec)) {
67 splice @$list, $i, 1, $rule;
80 my $rule = $validate_rule->(@_);
82 $insert_rule->($rule, $rule->[2] ? \@default_set_rules : \@default_mod_rules);
86 my ($class, %args) = @_;
88 my @set_rules = @default_set_rules;
89 my @mod_rules = @default_mod_rules;
91 my @user_rules = @{$args{rules} || []};
93 my ($target, $handler) = splice @user_rules, 0, 2;
95 my $rule = $validate_rule->($target, $handler);
97 $insert_rule->($rule, $rule->[2] ? \@set_rules : \@mod_rules);
100 my %dispatch = map { $_->[0] => $_ } @set_rules, @mod_rules;
107 $lts_tc->assert_valid($set);
109 my $rule = $dispatch{ref($set)};
111 ($set->isa($_->[0]) or $set->does($_->[0])) and $rule = $_ for @set_rules;
112 $rule = [ undef, sub { $_[1] } ] unless $rule;
114 my $new_set = $rule->[1]->($self, $set, @_);
115 my $is_new = $new_set ne $set;
119 for my $mod ($set->mods) {
120 my $rule = $dispatch{ref($mod)};
122 ($mod->isa($_->[0]) or $mod->does($_->[0])) and $rule = $_ for @mod_rules;
124 push @new_mods, $mod;
128 push @new_mods, $rule->[1]->($self, $mod, @_);
131 $new_set->mod(@new_mods) if $is_new;
137 use LaTeX::TikZ::Interface functor => sub {
140 __PACKAGE__->new(rules => \@_);
145 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
147 You can contact me by mail or on C<irc.perl.org> (vincent).
151 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>.
152 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
156 You can find documentation for this module with the perldoc command.
160 =head1 COPYRIGHT & LICENSE
162 Copyright 2010 Vincent Pit, all rights reserved.
164 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
168 1; # End of LaTeX::TikZ::Functor