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