]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Functor.pm
8c97432f2c57e1f7df9712b6cced21c4ffff30ef
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Functor.pm
1 package LaTeX::TikZ::Functor;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Functor - Build functor methods that recursively visit nodes of a LaTeX::TikZ::Set tree.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Carp ();
19
20 use Sub::Name ();
21
22 use LaTeX::TikZ::Interface;
23
24 use LaTeX::TikZ::Tools;
25
26 use Any::Moose 'Util' => [ 'does_role' ];
27
28 my $lts_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set');
29
30 my @default_set_rules;
31 my @default_mod_rules;
32
33 my ($validate_rule, $insert_rule);
34 BEGIN {
35  $validate_rule = Sub::Name::subname('validate_rule' => sub {
36   my ($target, $handler) = @_;
37
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");
41   }
42
43   (my $pm = $target) =~ s{::}{/}g;
44   $pm .= '.pm';
45   require $pm;
46
47   my $is_set;
48   if (does_role($target, 'LaTeX::TikZ::Set')) {
49    $is_set = 1;
50   } elsif (does_role($target, 'LaTeX::TikZ::Mod')) {
51    $is_set = 0;
52   } else {
53    Carp::confess("Target $target is neither a set nor a mod");
54   }
55
56   Carp::confess("Invalid handler for target $target")
57                                                   unless ref $handler eq 'CODE';
58
59   return [ $target, $handler, $is_set ];
60  });
61
62  $insert_rule = Sub::Name::subname('insert_rule' => sub {
63   my ($rule, $list) = @_;
64
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;
70     return 1;
71    }
72   }
73
74   push @$list, $rule;
75   return $#$list;
76  });
77 }
78
79 sub default_rule {
80  shift;
81
82  my $rule = $validate_rule->(@_);
83
84  $insert_rule->($rule, $rule->[2] ? \@default_set_rules : \@default_mod_rules);
85 }
86
87 sub new {
88  my ($class, %args) = @_;
89
90  my @set_rules = @default_set_rules;
91  my @mod_rules = @default_mod_rules;
92
93  my @user_rules = @{$args{rules} || []};
94  while (@user_rules) {
95   my ($target, $handler) = splice @user_rules, 0, 2;
96
97   my $rule = $validate_rule->($target, $handler);
98
99   $insert_rule->($rule, $rule->[2] ? \@set_rules : \@mod_rules);
100  }
101
102  my %dispatch  = map { $_->[0] => $_ } @set_rules, @mod_rules;
103
104  my $self;
105
106  $self = bless sub {
107   my $set = shift;
108
109   $lts_tc->assert_valid($set);
110
111   my $rule = $dispatch{ref($set)};
112   unless ($rule) {
113    ($set->isa($_->[0]) or $set->does($_->[0])) and $rule = $_ for @set_rules;
114    $rule = [ undef, sub { $_[1] } ] unless $rule;
115   }
116   my $new_set = $rule->[1]->($self, $set, @_);
117   my $is_new  = $new_set ne $set;
118
119   my @new_mods;
120 MOD:
121   for my $mod ($set->mods) {
122    my $rule = $dispatch{ref($mod)};
123    unless ($rule) {
124     ($mod->isa($_->[0]) or $mod->does($_->[0])) and $rule = $_ for @mod_rules;
125     unless ($rule) {
126      push @new_mods, $mod;
127      next MOD;
128     }
129    }
130    push @new_mods, $rule->[1]->($self, $mod, @_);
131   }
132
133   $new_set->mod(@new_mods) if $is_new;
134
135   return $new_set;
136  }, $class;
137 }
138
139 LaTeX::TikZ::Interface->register(
140  functor => sub {
141   shift;
142
143   __PACKAGE__->new(rules => \@_);
144  },
145 );
146
147 =head1 AUTHOR
148
149 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
150
151 You can contact me by mail or on C<irc.perl.org> (vincent).
152
153 =head1 BUGS
154
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.
157
158 =head1 SUPPORT
159
160 You can find documentation for this module with the perldoc command.
161
162     perldoc LaTeX::TikZ
163
164 =head1 COPYRIGHT & LICENSE
165
166 Copyright 2010 Vincent Pit, all rights reserved.
167
168 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
169
170 =cut
171
172 1; # End of LaTeX::TikZ::Functor