]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Sequence.pm
Introduce LaTeX::TikZ::Functor
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Sequence.pm
1 package LaTeX::TikZ::Set::Sequence;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set::Sequence - A set object grouping a sequence of objects.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use List::Util ();
19
20 use LaTeX::TikZ::Scope;
21
22 use LaTeX::TikZ::Functor;
23
24 use Any::Moose;
25 use Any::Moose 'Util::TypeConstraints'
26                => [ qw/subtype as where find_type_constraint/ ];
27
28 with qw(
29  LaTeX::TikZ::Set
30  LaTeX::TikZ::Set::Mutable
31 );
32
33 subtype 'LaTeX::TikZ::Set::Sequence::Elements'
34      => as 'Object'
35      => where {
36              $_->does('LaTeX::TikZ::Set::Op')
37           or $_->isa('LaTeX::TikZ::Set::Sequence')
38      };
39
40 has '_kids' => (
41  is       => 'ro',
42  isa      => 'Maybe[ArrayRef[LaTeX::TikZ::Set::Sequence::Elements]]',
43  init_arg => 'kids',
44  default  => sub { [ ] },
45 );
46
47 sub kids { @{$_[0]->_kids} }
48
49 my $ltsse_tc = find_type_constraint('LaTeX::TikZ::Set::Sequence::Elements');
50
51 sub add {
52  my $set = shift;
53
54  $ltsse_tc->assert_valid($_) for @_;
55
56  push @{$set->_kids}, @_;
57
58  $set;
59 }
60
61 sub draw {
62  my $set = shift;
63
64  List::Util::reduce { LaTeX::TikZ::Scope::fold($a, $b) }
65   map $_->draw(@_),
66    $set->kids;
67 }
68
69 use LaTeX::TikZ::Interface seq => sub {
70  shift;
71
72  __PACKAGE__->new(kids => \@_);
73 };
74
75 LaTeX::TikZ::Functor->default_rule(
76  (__PACKAGE__) => sub {
77   my ($functor, $set, @args) = @_;
78   $set->new(kids => [ map $_->$functor(@args), $set->kids ])
79  }
80 );
81
82 __PACKAGE__->meta->make_immutable;
83
84 =head1 AUTHOR
85
86 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
87
88 You can contact me by mail or on C<irc.perl.org> (vincent).
89
90 =head1 BUGS
91
92 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>.
93 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
94
95 =head1 SUPPORT
96
97 You can find documentation for this module with the perldoc command.
98
99     perldoc LaTeX::TikZ
100
101 =head1 COPYRIGHT & LICENSE
102
103 Copyright 2010 Vincent Pit, all rights reserved.
104
105 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
106
107 =cut
108
109 1; # End of LaTeX::TikZ::Set::Sequence