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;
7 our $AUTHORITY = 'cpan:STEVAN';
9 add_method_provider 'Collection::Array' => (
11 consumes => { 'Collection::List' => ':all' },
14 my ($attr, $reader, $writer) = @_;
15 return type_check($attr, sub {@_[1,$#_]}, sub {
17 CORE::push(@{ $reader->($self) }, @_);
22 my ($attr, $reader, $writer) = @_;
23 return sub { CORE::pop(@{ $reader->($_[0]) }) };
27 my ($attr, $reader, $writer) = @_;
28 return type_check($attr, sub {@_[1,$#_]}, sub {
30 CORE::unshift(@{ $reader->($self) }, @_);
35 my ($attr, $reader, $writer) = @_;
37 CORE::shift(@{ $reader->($_[0]) });
42 my ($attr, $reader, $writer) = @_;
45 return @{ $reader->($self) }[@_];
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;
59 my ($attr, $reader, $writer) = @_;
60 return sub { @{ $reader->($_[0]) } = () };
64 my ($attr, $reader, $writer) = @_;
66 CORE::splice(@{ $reader->($_[0]) }, $_[1], $_[2] || 1);
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);
88 MooseX::AttributeHelpers::MethodProvider::Array
92 This is a role which provides the method generators for
93 L<MooseX::AttributeHelpers::Collection::Array>.
95 =head1 PROVIDED METHODS
97 This module consumes L<MooseX::AttributeHelpers::MethodProvider::List>, and so
98 provides all of its methods as well. All methods work when multiple indexes
99 are supplied - special cases are noted.
103 =item B<get(@indexes)>
105 Behaves just like indexing an arrayref: returns the items indexed by the
106 supplied arguments (i.e. C<$self->get_my_stuff(1,2,3)> means
109 =item B<set($index, $value)>
111 =item B<set([$indexes], @values)>
113 This is just like assigning to an arrayref, except that an arrayref lets you
114 assign multiple indexes at once with no strange syntax. You can do that with
115 this set as well, but the first argument should be an arrayref of the keys you
116 want to assign to. (e.g. C<$self->set_aref([1,2,3], qw(foo bar baz))>)
130 =item B<unshift($item)>
136 Deletes all items from the array.
138 =item B<delete($index, $length)>
140 Deletes $length (default: 1) items from the array at $index.
142 =item B<insert($index, @items)>
144 Inserts @items into list at $index.
150 All complex software has bugs lurking in it, and this module is no
151 exception. If you find a bug please either email me, or add the bug
156 Stevan Little E<lt>stevan@iinteractive.comE<gt>
158 =head1 COPYRIGHT AND LICENSE
160 Copyright 2007-2008 by Infinity Interactive, Inc.
162 L<http://www.iinteractive.com>
164 This library is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself.