1 package Moose::AttributeHelpers::MethodProvider::Array;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
8 with 'Moose::AttributeHelpers::MethodProvider::List';
11 my ( $attr, $reader, $writer ) = @_;
14 $attr->has_type_constraint
15 && $attr->type_constraint->isa(
16 'Moose::Meta::TypeConstraint::Parameterized')
18 my $container_type_constraint
19 = $attr->type_constraint->type_parameter;
21 my $instance = CORE::shift;
22 $container_type_constraint->check($_)
25 . " did not pass container type constraint '$container_type_constraint'"
27 CORE::push @{ $reader->($instance) } => @_;
32 my $instance = CORE::shift;
33 CORE::push @{ $reader->($instance) } => @_;
39 my ( $attr, $reader, $writer ) = @_;
41 CORE::pop @{ $reader->( $_[0] ) };
45 sub unshift : method {
46 my ( $attr, $reader, $writer ) = @_;
48 $attr->has_type_constraint
49 && $attr->type_constraint->isa(
50 'Moose::Meta::TypeConstraint::Parameterized')
52 my $container_type_constraint
53 = $attr->type_constraint->type_parameter;
55 my $instance = CORE::shift;
56 $container_type_constraint->check($_)
59 . " did not pass container type constraint '$container_type_constraint'"
61 CORE::unshift @{ $reader->($instance) } => @_;
66 my $instance = CORE::shift;
67 CORE::unshift @{ $reader->($instance) } => @_;
73 my ( $attr, $reader, $writer ) = @_;
75 CORE::shift @{ $reader->( $_[0] ) };
80 my ( $attr, $reader, $writer ) = @_;
82 $reader->( $_[0] )->[ $_[1] ];
87 my ( $attr, $reader, $writer ) = @_;
89 $attr->has_type_constraint
90 && $attr->type_constraint->isa(
91 'Moose::Meta::TypeConstraint::Parameterized')
93 my $container_type_constraint
94 = $attr->type_constraint->type_parameter;
96 ( $container_type_constraint->check( $_[2] ) )
98 . ( $_[2] || 'undef' )
99 . " did not pass container type constraint '$container_type_constraint'";
100 $reader->( $_[0] )->[ $_[1] ] = $_[2];
105 $reader->( $_[0] )->[ $_[1] ] = $_[2];
110 sub accessor : method {
111 my ( $attr, $reader, $writer ) = @_;
114 $attr->has_type_constraint
115 && $attr->type_constraint->isa(
116 'Moose::Meta::TypeConstraint::Parameterized')
118 my $container_type_constraint
119 = $attr->type_constraint->type_parameter;
123 if ( @_ == 1 ) { # reader
124 return $reader->($self)->[ $_[0] ];
126 elsif ( @_ == 2 ) { # writer
127 ( $container_type_constraint->check( $_[1] ) )
129 . ( $_[1] || 'undef' )
130 . " did not pass container type constraint '$container_type_constraint'";
131 $reader->($self)->[ $_[0] ] = $_[1];
134 confess "One or two arguments expected, not " . @_;
142 if ( @_ == 1 ) { # reader
143 return $reader->($self)->[ $_[0] ];
145 elsif ( @_ == 2 ) { # writer
146 $reader->($self)->[ $_[0] ] = $_[1];
149 confess "One or two arguments expected, not " . @_;
156 my ( $attr, $reader, $writer ) = @_;
158 @{ $reader->( $_[0] ) } = ();
162 sub delete : method {
163 my ( $attr, $reader, $writer ) = @_;
165 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 1;
169 sub insert : method {
170 my ( $attr, $reader, $writer ) = @_;
172 $attr->has_type_constraint
173 && $attr->type_constraint->isa(
174 'Moose::Meta::TypeConstraint::Parameterized')
176 my $container_type_constraint
177 = $attr->type_constraint->type_parameter;
179 ( $container_type_constraint->check( $_[2] ) )
181 . ( $_[2] || 'undef' )
182 . " did not pass container type constraint '$container_type_constraint'";
183 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
188 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
193 sub splice : method {
194 my ( $attr, $reader, $writer ) = @_;
196 $attr->has_type_constraint
197 && $attr->type_constraint->isa(
198 'Moose::Meta::TypeConstraint::Parameterized')
200 my $container_type_constraint
201 = $attr->type_constraint->type_parameter;
203 my ( $self, $i, $j, @elems ) = @_;
204 ( $container_type_constraint->check($_) )
206 . ( defined($_) ? $_ : 'undef' )
207 . " did not pass container type constraint '$container_type_constraint'"
209 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
214 my ( $self, $i, $j, @elems ) = @_;
215 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
220 sub sort_in_place : method {
221 my ( $attr, $reader, $writer ) = @_;
223 my ( $instance, $predicate ) = @_;
225 die "Argument must be a code reference"
226 if $predicate && ref $predicate ne 'CODE';
230 @sorted = CORE::sort { $predicate->( $a, $b ) }
231 @{ $reader->($instance) };
234 @sorted = CORE::sort @{ $reader->($instance) };
237 $writer->( $instance, \@sorted );
249 Moose::AttributeHelpers::MethodProvider::Array
253 This is a role which provides the method generators for
254 L<Moose::AttributeHelpers::Collection::Array>.
264 =head1 PROVIDED METHODS
266 This module also consumes the B<List> method providers, to
267 see those provided methods, refer to that documentation.
291 =item B<sort_in_place>
293 Sorts the array I<in place>, modifying the value of the attribute.
295 You can provide an optional subroutine reference to sort with (as you
296 can with the core C<sort> function). However, instead of using C<$a>
297 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
301 If passed one argument, returns the value of the requested element.
302 If passed two arguments, sets the value of the requested element.
308 All complex software has bugs lurking in it, and this module is no
309 exception. If you find a bug please either email me, or add the bug
314 Stevan Little E<lt>stevan@iinteractive.comE<gt>
316 =head1 COPYRIGHT AND LICENSE
318 Copyright 2007-2009 by Infinity Interactive, Inc.
320 L<http://www.iinteractive.com>
322 This library is free software; you can redistribute it and/or modify
323 it under the same terms as Perl itself.