1 package Moose::Meta::Attribute::Native::MethodProvider::Array;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
9 my ( $attr, $reader, $writer ) = @_;
11 scalar @{ $reader->( $_[0] ) };
16 my ( $attr, $reader, $writer ) = @_;
18 scalar @{ $reader->( $_[0] ) } ? 0 : 1;
23 my ( $attr, $reader, $writer ) = @_;
25 my ( $instance, $predicate ) = @_;
26 foreach my $val ( @{ $reader->($instance) } ) {
27 return $val if $predicate->($val);
34 my ( $attr, $reader, $writer ) = @_;
36 my ( $instance, $f ) = @_;
37 CORE::map { $f->($_) } @{ $reader->($instance) };
42 my ( $attr, $reader, $writer ) = @_;
44 my ( $instance, $predicate ) = @_;
45 die "Argument must be a code reference"
46 if $predicate && ref $predicate ne 'CODE';
49 CORE::sort { $predicate->( $a, $b ) } @{ $reader->($instance) };
52 CORE::sort @{ $reader->($instance) };
58 my ( $attr, $reader, $writer ) = @_;
60 my ( $instance, $predicate ) = @_;
61 CORE::grep { $predicate->($_) } @{ $reader->($instance) };
65 sub elements : method {
66 my ( $attr, $reader, $writer ) = @_;
69 @{ $reader->($instance) };
74 my ( $attr, $reader, $writer ) = @_;
76 my ( $instance, $separator ) = @_;
77 join $separator, @{ $reader->($instance) };
82 my ( $attr, $reader, $writer ) = @_;
84 $reader->( $_[0] )->[0];
89 my ( $attr, $reader, $writer ) = @_;
91 my $arr = $reader->( $_[0] );
92 return @{ $arr }[1..$#{ $arr }];
97 my ( $attr, $reader, $writer ) = @_;
99 $reader->( $_[0] )->[-1];
104 my ( $attr, $reader, $writer ) = @_;
107 $attr->has_type_constraint
108 && $attr->type_constraint->isa(
109 'Moose::Meta::TypeConstraint::Parameterized')
112 my $container_type_constraint = $attr->type_constraint->type_parameter;
114 my $instance = CORE::shift;
115 $container_type_constraint->check($_)
118 . " did not pass container type constraint '$container_type_constraint'"
120 CORE::push @{ $reader->($instance) } => @_;
125 my $instance = CORE::shift;
126 CORE::push @{ $reader->($instance) } => @_;
132 my ( $attr, $reader, $writer ) = @_;
134 CORE::pop @{ $reader->( $_[0] ) };
138 sub unshift : method {
139 my ( $attr, $reader, $writer ) = @_;
141 $attr->has_type_constraint
142 && $attr->type_constraint->isa(
143 'Moose::Meta::TypeConstraint::Parameterized')
146 my $container_type_constraint = $attr->type_constraint->type_parameter;
148 my $instance = CORE::shift;
149 $container_type_constraint->check($_)
152 . " did not pass container type constraint '$container_type_constraint'"
154 CORE::unshift @{ $reader->($instance) } => @_;
159 my $instance = CORE::shift;
160 CORE::unshift @{ $reader->($instance) } => @_;
166 my ( $attr, $reader, $writer ) = @_;
168 CORE::shift @{ $reader->( $_[0] ) };
173 my ( $attr, $reader, $writer ) = @_;
175 $reader->( $_[0] )->[ $_[1] ];
180 my ( $attr, $reader, $writer ) = @_;
182 $attr->has_type_constraint
183 && $attr->type_constraint->isa(
184 'Moose::Meta::TypeConstraint::Parameterized')
187 my $container_type_constraint = $attr->type_constraint->type_parameter;
189 ( $container_type_constraint->check( $_[2] ) )
191 . ( $_[2] || 'undef' )
192 . " did not pass container type constraint '$container_type_constraint'";
193 $reader->( $_[0] )->[ $_[1] ] = $_[2];
198 $reader->( $_[0] )->[ $_[1] ] = $_[2];
203 sub accessor : method {
204 my ( $attr, $reader, $writer ) = @_;
207 $attr->has_type_constraint
208 && $attr->type_constraint->isa(
209 'Moose::Meta::TypeConstraint::Parameterized')
212 my $container_type_constraint = $attr->type_constraint->type_parameter;
216 if ( @_ == 1 ) { # reader
217 return $reader->($self)->[ $_[0] ];
219 elsif ( @_ == 2 ) { # writer
220 ( $container_type_constraint->check( $_[1] ) )
222 . ( $_[1] || 'undef' )
223 . " did not pass container type constraint '$container_type_constraint'";
224 $reader->($self)->[ $_[0] ] = $_[1];
227 confess "One or two arguments expected, not " . @_;
235 if ( @_ == 1 ) { # reader
236 return $reader->($self)->[ $_[0] ];
238 elsif ( @_ == 2 ) { # writer
239 $reader->($self)->[ $_[0] ] = $_[1];
242 confess "One or two arguments expected, not " . @_;
249 my ( $attr, $reader, $writer ) = @_;
251 @{ $reader->( $_[0] ) } = ();
255 sub delete : method {
256 my ( $attr, $reader, $writer ) = @_;
258 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 1;
262 sub insert : method {
263 my ( $attr, $reader, $writer ) = @_;
265 $attr->has_type_constraint
266 && $attr->type_constraint->isa(
267 'Moose::Meta::TypeConstraint::Parameterized')
270 my $container_type_constraint = $attr->type_constraint->type_parameter;
272 ( $container_type_constraint->check( $_[2] ) )
274 . ( $_[2] || 'undef' )
275 . " did not pass container type constraint '$container_type_constraint'";
276 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
281 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
286 sub splice : method {
287 my ( $attr, $reader, $writer ) = @_;
289 $attr->has_type_constraint
290 && $attr->type_constraint->isa(
291 'Moose::Meta::TypeConstraint::Parameterized')
294 my $container_type_constraint = $attr->type_constraint->type_parameter;
296 my ( $self, $i, $j, @elems ) = @_;
297 ( $container_type_constraint->check($_) )
299 . ( defined($_) ? $_ : 'undef' )
300 . " did not pass container type constraint '$container_type_constraint'"
302 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
307 my ( $self, $i, $j, @elems ) = @_;
308 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
313 sub sort_in_place : method {
314 my ( $attr, $reader, $writer ) = @_;
316 my ( $instance, $predicate ) = @_;
318 die "Argument must be a code reference"
319 if $predicate && ref $predicate ne 'CODE';
324 CORE::sort { $predicate->( $a, $b ) } @{ $reader->($instance) };
327 @sorted = CORE::sort @{ $reader->($instance) };
330 $writer->( $instance, \@sorted );
342 Moose::Meta::Attribute::Native::MethodProvider::Array
346 This is a role which provides the method generators for
347 L<Moose::Meta::Attribute::Trait::Native::Array>. Please check there for
348 documentation on what methods are provided.
360 All complex software has bugs lurking in it, and this module is no
361 exception. If you find a bug please either email me, or add the bug
366 Stevan Little E<lt>stevan@iinteractive.comE<gt>
368 =head1 COPYRIGHT AND LICENSE
370 Copyright 2007-2009 by Infinity Interactive, Inc.
372 L<http://www.iinteractive.com>
374 This library is free software; you can redistribute it and/or modify
375 it under the same terms as Perl itself.