Docs tentatively finished.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Collection / Array.pm
CommitLineData
1b45ecc4 1package MooseX::AttributeHelpers::MethodProvider::Collection::Array;
2use MooseX::AttributeHelpers::MethodProvider;
3use MooseX::AttributeHelpers::MethodProvider::Util qw(type_check);
4use MooseX::AttributeHelpers::MethodProvider::Collection::List;
5
6our $VERSION = '0.05';
7our $AUTHORITY = 'cpan:STEVAN';
8
9add_method_provider 'Collection::Array' => (
10 type => 'ArrayRef',
11 consumes => { 'Collection::List' => ':all' },
12 provides => {
13 push => sub {
14 my ($attr, $reader, $writer) = @_;
15 return type_check($attr, sub {@_[1,$#_]}, sub {
16 my $self = shift;
17 CORE::push(@{ $reader->($self) }, @_);
18 });
19 },
20
21 pop => sub {
22 my ($attr, $reader, $writer) = @_;
23 return sub { CORE::pop(@{ $reader->($_[0]) }) };
24 },
25
26 unshift => sub {
27 my ($attr, $reader, $writer) = @_;
28 return type_check($attr, sub {@_[1,$#_]}, sub {
29 my $self = shift;
30 CORE::unshift(@{ $reader->($self) }, @_);
31 });
32 },
33
34 shift => sub {
35 my ($attr, $reader, $writer) = @_;
36 return sub {
37 CORE::shift(@{ $reader->($_[0]) });
38 };
39 },
40
41 get => sub {
42 my ($attr, $reader, $writer) = @_;
43 return sub {
44 my $self = shift;
45 return @{ $reader->($self) }[@_];
46 };
47 },
48
49 set => sub {
50 my ($attr, $reader, $writer) = @_;
51 return type_check($attr, sub {@_[2,$#_]}, sub {
52 my ($self, $index, @values) = @_;
53 my @indexes = (ref $index eq 'ARRAY' ? @$index : ($index));
54 @{ $reader->($self) }[@indexes] = @values;
55 });
56 },
57
58 clear => sub {
59 my ($attr, $reader, $writer) = @_;
60 return sub { @{ $reader->($_[0]) } = () };
61 },
62
63 delete => sub {
64 my ($attr, $reader, $writer) = @_;
65 return sub {
66 CORE::splice(@{ $reader->($_[0]) }, $_[1], $_[2] || 1);
67 };
68 },
69
70 insert => sub {
71 my ($attr, $reader, $writer) = @_;
72 return type_contraint($attr, sub {@_[2,$#_]}, sub {
73 my ($self, $index, @values) = @_;
74 CORE::splice(@{ $reader->($self) }, $index, 0, @values);
75 });
76 },
77 },
78);
79
801;
81
82__END__
83
84=pod
85
86=head1 NAME
87
1d458eb8 88MooseX::AttributeHelpers::MethodProvider::Collection::Array
1b45ecc4 89
90=head1 DESCRIPTION
91
1d458eb8 92This module provides the method factories for
93L<MooseX::AttributeHelpers::Collection::Array>. It also consumes
94L<MooseX::AttributeHelpers::MethodProvider::Collection::List>, and thus
95provides all its methods as well.
1b45ecc4 96
97=head1 PROVIDED METHODS
98
99This module consumes L<MooseX::AttributeHelpers::MethodProvider::List>, and so
100provides all of its methods as well. All methods work when multiple indexes
101are supplied - special cases are noted.
102
103=over 4
104
105=item B<get(@indexes)>
106
107Behaves just like indexing an arrayref: returns the items indexed by the
108supplied arguments (i.e. C<$self-&gt;get_my_stuff(1,2,3)> means
109C<@{$aref}[1,2,3]>).
110
111=item B<set($index, $value)>
112
113=item B<set([$indexes], @values)>
114
115This is just like assigning to an arrayref, except that an arrayref lets you
116assign multiple indexes at once with no strange syntax. You can do that with
117this set as well, but the first argument should be an arrayref of the keys you
118want to assign to. (e.g. C<$self-&gt;set_aref([1,2,3], qw(foo bar baz))>)
119
120=item B<pop>
121
122L<perlfunc/pop>
123
124=item B<push($item)>
125
126L<perlfunc/push>
127
128=item B<shift>
129
130L<perlfunc/shift>
131
132=item B<unshift($item)>
133
134L<perlfunc/unshift>
135
136=item B<clear>
137
138Deletes all items from the array.
139
140=item B<delete($index, $length)>
141
142Deletes $length (default: 1) items from the array at $index.
143
144=item B<insert($index, @items)>
145
146Inserts @items into list at $index.
147
148=back
149
150=head1 BUGS
151
152All complex software has bugs lurking in it, and this module is no
153exception. If you find a bug please either email me, or add the bug
154to cpan-RT.
155
156=head1 AUTHOR
157
158Stevan Little E<lt>stevan@iinteractive.comE<gt>
159
160=head1 COPYRIGHT AND LICENSE
161
162Copyright 2007-2008 by Infinity Interactive, Inc.
163
164L<http://www.iinteractive.com>
165
166This library is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself.
168
169=cut