From: Vincent Pit Date: Tue, 1 Feb 2011 13:00:51 +0000 (+0100) Subject: Generate the chain links only at path time X-Git-Tag: rt87282~15 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=e8590b71a4239eabe4a13e86163b1ed8dad695ed;p=perl%2Fmodules%2FLaTeX-TikZ.git Generate the chain links only at path time So that they can get the formatter object. --- diff --git a/lib/LaTeX/TikZ/Set/Chain.pm b/lib/LaTeX/TikZ/Set/Chain.pm index e12587e..2c640c2 100644 --- a/lib/LaTeX/TikZ/Set/Chain.pm +++ b/lib/LaTeX/TikZ/Set/Chain.pm @@ -68,7 +68,7 @@ sub kids { @{$_[0]->_kids} } =head2 C A code reference that describes how two successive elements of the chain are linked. -It is called repeatedly with these arguments : +When the L method is , the connector is run repeatedly with these arguments : =over 4 @@ -88,6 +88,10 @@ The C<$i>-th L object in the chain. The C<$i+1>-th L object in the chain. +=item * + +The L object. + =back 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. @@ -112,13 +116,6 @@ has 'connector' => ( coerce => 1, ); -has '_links' => ( - is => 'ro', - isa => 'ArrayRef[Str]', - init_arg => undef, - default => sub { [ ] }, -); - =head1 METHODS =head2 C @@ -131,65 +128,35 @@ sub add { my $set = shift; $ltsp_tc->assert_valid($_) for @_; - return $set unless @_; - my $kids = $set->_kids; - my $links = $set->_links; - my $conn = $set->connector; - - push @$kids, shift @_ unless @$kids; - return $set unless @_; - - my $prev = $kids->[-1]; - for my $i (0 .. $#_) { - my $next = $_[$i]; - my $link = $set->$conn($#$kids, $prev, $next); - confess('Invalid connector') unless defined $link and not blessed $link; - push @$links, $link; - push @$kids, $next; - $prev = $next; - } + push @{$set->_kids}, @_; $set; } -sub BUILD { - my ($set) = @_; - - my @kids = $set->kids; - return unless @kids >= 2; - - my $links = $set->_links; - my $conn = $set->connector; - - my $prev = $kids[0]; - for my $i (1 .. $#kids) { - my $next = $_[$i]; - my $link = $set->$conn($i - 1, $prev, $next); - confess('Invalid connector') unless defined $link and not blessed $link; - push @$links, $link; - $prev = $next; - } -} - =head2 C =cut sub path { - my $set = shift; + my ($set, $tikz) = @_; my @kids = $set->kids; return '' unless @kids; - my $links = $set->_links; my $conn = $set->connector; - my $path = $kids[0]->path(@_); + my $prev = $kids[0]; + my $path = $prev->path($tikz); + for my $i (1 .. $#kids) { - my $link = ' ' . $links->[$i - 1] . ' '; + my $next = $kids[$i]; + my $link = $set->$conn($i - 1, $prev, $next, $tikz); + confess('Invalid connector') unless defined $link and not blessed $link; + $link = " $link "; $link =~ s/\s+/ /g; - $path .= $link . $kids[$i]->path(@_); + $path .= $link . $next->path($tikz); + $prev = $next; } return $path; diff --git a/t/10-set.t b/t/10-set.t index 1476558..e385932 100644 --- a/t/10-set.t +++ b/t/10-set.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 28 + 2 * 21; +use Test::More tests => 29 + 2 * 21; use LaTeX::TikZ; @@ -244,6 +244,12 @@ RES eval { $join->add($bar); }; +is $@, '', + 'adding too many sets to a chain set joined with an arrayref doesn\'t croak'; + +eval { + using()->render($join); +}; like $@, qr/^Invalid connector/, 'adding too many sets to a chain set joined with an arrayref croaks';