1 package LaTeX::TikZ::Set::Chain;
8 LaTeX::TikZ::Set::Chain - A set object representing a connected path between several objects.
16 our $VERSION = '0.02';
18 use LaTeX::TikZ::Set::Point;
20 use LaTeX::TikZ::Interface;
21 use LaTeX::TikZ::Functor;
23 use LaTeX::TikZ::Tools;
26 use Any::Moose 'Util::TypeConstraints' => [ qw<subtype as coerce from via> ];
30 This class consumes the L<LaTeX::TikZ::Set::Path> and L<LaTeX::TikZ::Set::Container> roles, and as such implements the L</path>, L</kids> and L</add> methods.
35 LaTeX::TikZ::Set::Path
36 LaTeX::TikZ::Set::Container
43 The L<LaTeX::TikZ::Set::Path> objects that form the chain.
47 subtype 'LaTeX::TikZ::Set::Chain::Elements'
48 => as 'ArrayRef[LaTeX::TikZ::Set::Path]';
50 coerce 'LaTeX::TikZ::Set::Chain::Elements'
51 => from 'ArrayRef[Any]'
53 blessed($_) && $_->does('LaTeX::TikZ::Set')
55 : LaTeX::TikZ::Set::Point->new(point => $_)
60 isa => 'LaTeX::TikZ::Set::Chain::Elements',
62 default => sub { [ ] },
66 sub kids { @{$_[0]->_kids} }
70 A code reference that describes how two successive elements of the chain are linked.
71 When the L</path> method is , the connector is run repeatedly with these arguments :
77 The current L<LaTeX::TikZ::Set::Chain> object.
81 The index C<$i> of the current position in the chain, starting at C<0> for the link between the two first elements.
85 The C<$i>-th L<LaTeX::TikZ::Set> object in the chain.
89 The C<$i+1>-th L<LaTeX::TikZ::Set> object in the chain.
93 The L<LaTeX::TikZ::Formatter> object.
97 You can also pass a string, which will be upgraded to a code reference constantly returning that string ; or an array reference, which will be turned into a code reference returning the C<$i>-th element of the array when asked for the C<$i>-th link.
101 subtype 'LaTeX::TikZ::Set::Chain::Connector'
104 coerce 'LaTeX::TikZ::Set::Chain::Connector'
106 => via { my $conn = $_; sub { $conn } };
108 coerce 'LaTeX::TikZ::Set::Chain::Connector'
109 => from 'ArrayRef[Str]'
110 => via { my $conns = $_; sub { $conns->[$_[1]] } };
114 isa => 'LaTeX::TikZ::Set::Chain::Connector',
125 my $ltsp_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set::Path');
130 $ltsp_tc->assert_valid($_) for @_;
132 push @{$set->_kids}, @_;
142 my ($set, $tikz) = @_;
144 my @kids = $set->kids;
145 return '' unless @kids;
147 my $conn = $set->connector;
150 my $path = $prev->path($tikz);
152 for my $i (1 .. $#kids) {
153 my $next = $kids[$i];
154 my $link = $set->$conn($i - 1, $prev, $next, $tikz);
155 confess('Invalid connector') unless defined $link and not blessed $link;
158 $path .= $link . $next->path($tikz);
172 my @kids = $set->kids;
173 return undef unless @kids;
185 my @kids = $set->kids;
186 return undef unless @kids;
191 LaTeX::TikZ::Interface->register(
203 confess("The 'chain' command expects an odd number of arguments")
208 for (my $i = 0; $i < @_; $i += 2) {
210 push @kids, $_[$i + 1];
215 connector => \@links,
220 LaTeX::TikZ::Functor->default_rule(
221 (__PACKAGE__) => sub {
222 my ($functor, $set, @args) = @_;
224 kids => [ map $_->$functor(@args), $set->kids ],
225 connector => $set->connector,
230 __PACKAGE__->meta->make_immutable;
234 L<LaTeX::TikZ>, L<LaTeX::TikZ::Set::Path>.
238 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
240 You can contact me by mail or on C<irc.perl.org> (vincent).
244 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>.
245 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
249 You can find documentation for this module with the perldoc command.
253 =head1 COPYRIGHT & LICENSE
255 Copyright 2011 Vincent Pit, all rights reserved.
257 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
261 1; # End of LaTeX::TikZ::Set::Chain