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