1 package LaTeX::TikZ::Functor::Rule;
8 LaTeX::TikZ::Functor::Rule - An object that specifies how functors should handle a certain kind of set or mod.
16 our $VERSION = '0.01';
20 A rule specifies how functors should handle a certain kind of set or mod.
21 A functor is basically an ordered collection of rules.
28 use Any::Moose 'Util' => [ qw[find_meta does_role] ];
29 use Any::Moose 'Util::TypeConstraints';
39 isa => 'ClassName|RoleName',
73 my $ltfrl_tc = subtype 'LaTeX::TikZ::Functor::RuleList'
74 => as 'ArrayRef[LaTeX::TikZ::Functor::Rule]';
78 =head2 C<< new target => $target, handler => $handler >>
82 around 'BUILDARGS' => sub {
83 my ($orig, $class, %args) = @_;
85 my $target = $args{target};
86 __PACKAGE__->meta->find_attribute_by_name('target')
87 ->type_constraint->assert_valid($target);
89 (my $pm = $target) =~ s{::}{/}g;
93 my $meta = find_meta($target);
94 Carp::confess("No meta object associated with target $target")
96 $args{is_role} = $meta->isa(any_moose('Meta::Role'));
99 if (does_role($target, 'LaTeX::TikZ::Set')) {
101 } elsif (does_role($target, 'LaTeX::TikZ::Mod')) {
104 Carp::confess("Target $target is neither a set nor a mod");
106 $args{is_set} = $is_set;
108 $class->$orig(%args);
111 =head2 C<< insert into => \@list, overwrite => $overwrite, replace => $replace >>
113 Inserts the current rule into the list of rules C<@list>.
115 If C<$replace> is false, then the rule will be appended to the C<@list> ; except if there already is an existent entry for the same target, in which case it will be overwritten if C<$overwrite> is true, or an exception will be thrown if it is false.
117 If C<$replace> is true, then the rule will replace the first rule in the list that is a subclass or that consumes the role denoted by the target.
118 All the subsequent rules in the list that inherit or consume the target will be removed.
123 my ($rule, %args) = @_;
125 my $list = $args{into};
126 $ltfrl_tc->assert_valid($list);
128 my $overwrite = $args{overwrite};
129 my $replace = $args{replace};
131 my $target = $rule->target;
132 my $is_role = $rule->is_role;
137 for my $i (0 .. $#$list) {
138 my $old_target = $list->[$i]->target;
139 if ($rule->handles($old_target)) {
141 splice @$list, $i, 1, $rule;
151 splice @$list, $_ - $shift, 1;
154 return 1 unless defined $rule;
156 } else { # Replace only an existent rule
158 for my $i (0 .. $#$list) {
159 my $old_target = $list->[$i]->target;
160 if ($old_target eq $target) {
161 Carp::confess("Default rule already defined for target $target")
163 splice @$list, $i, 1, $rule;
173 =head2 C<handles $obj>
175 Returns true if and only if the current rule can handle the object or class/role name C<$obj>.
180 my ($rule, $obj) = @_;
182 $rule->is_role ? does_role($obj, $rule->target) : $obj->isa($rule->target);
185 __PACKAGE__->meta->make_immutable;
189 L<LaTeX::TikZ>, L<LaTeX::TikZ::Functor>.
193 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
195 You can contact me by mail or on C<irc.perl.org> (vincent).
199 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>.
200 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
204 You can find documentation for this module with the perldoc command.
208 =head1 COPYRIGHT & LICENSE
210 Copyright 2010 Vincent Pit, all rights reserved.
212 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
216 1; # End of LaTeX::TikZ::Functor::Rule