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;
19 use LaTeX::TikZ::Set::Raw;
21 use LaTeX::TikZ::Interface;
22 use LaTeX::TikZ::Functor;
24 use LaTeX::TikZ::Tools;
27 use Mouse::Util::TypeConstraints qw<subtype as coerce from via>;
31 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.
36 LaTeX::TikZ::Set::Path
37 LaTeX::TikZ::Set::Container
44 The L<LaTeX::TikZ::Set::Path> objects that form the chain.
48 subtype 'LaTeX::TikZ::Set::Chain::Elements'
49 => as 'ArrayRef[LaTeX::TikZ::Set::Path]';
51 coerce 'LaTeX::TikZ::Set::Chain::Elements'
52 => from 'ArrayRef[Any]'
54 blessed($_) && $_->does('LaTeX::TikZ::Set')
56 : LaTeX::TikZ::Set::Point->new(point => $_)
61 isa => 'LaTeX::TikZ::Set::Chain::Elements',
63 default => sub { [ ] },
67 sub kids { @{$_[0]->_kids} }
71 A code reference that describes how two successive elements of the chain are linked.
72 When the L</path> method is called, the connector is run repeatedly with these arguments :
78 The current L<LaTeX::TikZ::Set::Chain> object.
82 The index C<$i> of the current position in the chain, starting at C<0> for the link between the two first elements.
86 The C<$i>-th L<LaTeX::TikZ::Set> object in the chain.
90 The C<$i+1>-th L<LaTeX::TikZ::Set> object in the chain.
94 The L<LaTeX::TikZ::Formatter> object.
98 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.
102 subtype 'LaTeX::TikZ::Set::Chain::Connector'
105 coerce 'LaTeX::TikZ::Set::Chain::Connector'
107 => via { my $conn = $_; sub { $conn } };
109 coerce 'LaTeX::TikZ::Set::Chain::Connector'
110 => from 'ArrayRef[Str]'
111 => via { my $conns = $_; sub { $conns->[$_[1]] } };
115 isa => 'LaTeX::TikZ::Set::Chain::Connector',
122 A boolean that indicates whether the path is a cycle or not.
138 my $ltsp_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set::Path');
143 $ltsp_tc->assert_valid($_) for @_;
145 push @{$set->_kids}, @_;
157 my @kids = $set->kids;
158 return '' unless @kids;
160 my $conn = $set->connector;
163 my $path = $prev->path(@_);
166 push @kids, LaTeX::TikZ::Set::Raw->new(
172 for my $i (1 .. $#kids) {
173 my $next = $kids[$i];
174 my $link = $set->$conn($i - 1, $prev, $next, $tikz);
175 confess('Invalid connector') unless defined $link and not blessed $link;
178 $path .= $link . $next->path(@_);
192 my @kids = $set->kids;
193 return undef unless @kids;
205 my @kids = $set->kids;
206 return undef unless @kids;
211 LaTeX::TikZ::Interface->register(
223 confess("The 'chain' command expects an odd number of arguments")
228 for (my $i = 0; $i < @_; $i += 2) {
230 push @kids, $_[$i + 1];
235 connector => \@links,
240 LaTeX::TikZ::Functor->default_rule(
241 (__PACKAGE__) => sub {
242 my ($functor, $set, @args) = @_;
244 kids => [ map $_->$functor(@args), $set->kids ],
245 connector => $set->connector,
246 cycle => $set->cycle,
251 __PACKAGE__->meta->make_immutable;
255 L<LaTeX::TikZ>, L<LaTeX::TikZ::Set::Path>.
259 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
261 You can contact me by mail or on C<irc.perl.org> (vincent).
265 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>.
266 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
270 You can find documentation for this module with the perldoc command.
274 =head1 COPYRIGHT & LICENSE
276 Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
278 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
282 1; # End of LaTeX::TikZ::Set::Chain