Docs tentatively finished.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Collection / Array.pm
1 package MooseX::AttributeHelpers::MethodProvider::Collection::Array;
2 use MooseX::AttributeHelpers::MethodProvider;
3 use MooseX::AttributeHelpers::MethodProvider::Util qw(type_check);
4 use MooseX::AttributeHelpers::MethodProvider::Collection::List;
5
6 our $VERSION   = '0.05';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 add_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
80 1;
81
82 __END__
83
84 =pod
85
86 =head1 NAME
87
88 MooseX::AttributeHelpers::MethodProvider::Collection::Array
89
90 =head1 DESCRIPTION
91
92 This module provides the method factories for
93 L<MooseX::AttributeHelpers::Collection::Array>.  It also consumes
94 L<MooseX::AttributeHelpers::MethodProvider::Collection::List>, and thus
95 provides all its methods as well.
96
97 =head1 PROVIDED METHODS
98
99 This module consumes L<MooseX::AttributeHelpers::MethodProvider::List>, and so
100 provides all of its methods as well.  All methods work when multiple indexes
101 are supplied - special cases are noted.
102
103 =over 4
104
105 =item B<get(@indexes)>
106
107 Behaves just like indexing an arrayref: returns the items indexed by the
108 supplied arguments (i.e. C<$self-&gt;get_my_stuff(1,2,3)> means
109 C<@{$aref}[1,2,3]>).
110
111 =item B<set($index, $value)>
112
113 =item B<set([$indexes], @values)>
114
115 This is just like assigning to an arrayref, except that an arrayref lets you
116 assign multiple indexes at once with no strange syntax.  You can do that with
117 this set as well, but the first argument should be an arrayref of the keys you
118 want to assign to.  (e.g. C<$self-&gt;set_aref([1,2,3], qw(foo bar baz))>)
119
120 =item B<pop>
121
122 L<perlfunc/pop>
123
124 =item B<push($item)>
125
126 L<perlfunc/push>
127
128 =item B<shift>
129
130 L<perlfunc/shift>
131
132 =item B<unshift($item)>
133
134 L<perlfunc/unshift>
135
136 =item B<clear>
137
138 Deletes all items from the array.
139
140 =item B<delete($index, $length)>
141
142 Deletes $length (default: 1) items from the array at $index.
143
144 =item B<insert($index, @items)>
145
146 Inserts @items into list at $index.
147
148 =back
149
150 =head1 BUGS
151
152 All complex software has bugs lurking in it, and this module is no
153 exception. If you find a bug please either email me, or add the bug
154 to cpan-RT.
155
156 =head1 AUTHOR
157
158 Stevan Little E<lt>stevan@iinteractive.comE<gt>
159
160 =head1 COPYRIGHT AND LICENSE
161
162 Copyright 2007-2008 by Infinity Interactive, Inc.
163
164 L<http://www.iinteractive.com>
165
166 This library is free software; you can redistribute it and/or modify
167 it under the same terms as Perl itself.
168
169 =cut