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 It is called 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 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.
97 subtype 'LaTeX::TikZ::Set::Chain::Connector'
100 coerce 'LaTeX::TikZ::Set::Chain::Connector'
102 => via { my $conn = $_; sub { $conn } };
104 coerce 'LaTeX::TikZ::Set::Chain::Connector'
105 => from 'ArrayRef[Str]'
106 => via { my $conns = $_; sub { $conns->[$_[1]] } };
110 isa => 'LaTeX::TikZ::Set::Chain::Connector',
117 isa => 'ArrayRef[Str]',
119 default => sub { [ ] },
128 my $ltsp_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set::Path');
133 $ltsp_tc->assert_valid($_) for @_;
134 return $set unless @_;
136 my $kids = $set->_kids;
137 my $links = $set->_links;
138 my $conn = $set->connector;
140 push @$kids, shift @_ unless @$kids;
141 return $set unless @_;
143 my $prev = $kids->[-1];
144 for my $i (0 .. $#_) {
146 my $link = $set->$conn($#$kids, $prev, $next);
147 confess('Invalid connector') unless defined $link and not blessed $link;
159 my @kids = $set->kids;
160 return unless @kids >= 2;
162 my $links = $set->_links;
163 my $conn = $set->connector;
166 for my $i (1 .. $#kids) {
168 my $link = $set->$conn($i - 1, $prev, $next);
169 confess('Invalid connector') unless defined $link and not blessed $link;
182 my @kids = $set->kids;
183 return '' unless @kids;
185 my $links = $set->_links;
186 my $conn = $set->connector;
188 my $path = $kids[0]->path(@_);
189 for my $i (1 .. $#kids) {
190 my $link = ' ' . $links->[$i - 1] . ' ';
192 $path .= $link . $kids[$i]->path(@_);
205 my @kids = $set->kids;
206 return undef unless @kids;
218 my @kids = $set->kids;
219 return undef unless @kids;
224 LaTeX::TikZ::Interface->register(
236 confess("The 'chain' command expects an odd number of arguments")
241 for (my $i = 0; $i < @_; $i += 2) {
243 push @kids, $_[$i + 1];
248 connector => \@links,
253 LaTeX::TikZ::Functor->default_rule(
254 (__PACKAGE__) => sub {
255 my ($functor, $set, @args) = @_;
257 kids => [ map $_->$functor(@args), $set->kids ],
258 connector => $set->connector,
263 __PACKAGE__->meta->make_immutable;
267 L<LaTeX::TikZ>, L<LaTeX::TikZ::Set::Path>.
271 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
273 You can contact me by mail or on C<irc.perl.org> (vincent).
277 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>.
278 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
282 You can find documentation for this module with the perldoc command.
286 =head1 COPYRIGHT & LICENSE
288 Copyright 2011 Vincent Pit, all rights reserved.
290 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
294 1; # End of LaTeX::TikZ::Set::Chain