1 package Moose::Meta::Attribute::Native::MethodProvider::Array;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
12 my ( $attr, $reader, $writer ) = @_;
14 my ( $instance, $f ) = @_;
16 List::Util::reduce { $f->($a, $b) } @{ $reader->($instance) };
21 my ( $attr, $reader, $writer ) = @_;
23 my ( $instance, $predicate ) = @_;
24 die "Argument must be a code reference"
25 if $predicate && ref $predicate ne 'CODE';
28 # Although it would be nice if we could support just using $a and
29 # $b like sort already does, using $a or $b once in a package
30 # triggers the 'Name "main::a" used only once' warning, and there
31 # is no good way to avoid that, since it happens when the file
32 # which defines the coderef is compiled, before we even get a
33 # chance to see it here. So, we have no real choice but to use
34 # normal parameters. --doy
35 CORE::sort { $predicate->( $a, $b ) } @{ $reader->($instance) };
38 CORE::sort @{ $reader->($instance) };
43 sub shuffle : method {
44 my ( $attr, $reader, $writer ) = @_;
46 my ( $instance ) = @_;
47 List::Util::shuffle @{ $reader->($instance) };
52 my ( $attr, $reader, $writer ) = @_;
54 my ( $instance, $predicate ) = @_;
55 CORE::grep { $predicate->() } @{ $reader->($instance) };
60 my ( $attr, $reader, $writer ) = @_;
62 my ( $instance ) = @_;
63 List::MoreUtils::uniq @{ $reader->($instance) };
68 my ( $attr, $reader, $writer ) = @_;
70 my ( $instance, $separator ) = @_;
71 join $separator, @{ $reader->($instance) };
76 my ( $attr, $reader, $writer ) = @_;
79 $attr->has_type_constraint
80 && $attr->type_constraint->isa(
81 'Moose::Meta::TypeConstraint::Parameterized')
84 my $container_type_constraint = $attr->type_constraint->type_parameter;
86 my $instance = CORE::shift;
87 $container_type_constraint->check($_)
90 . " did not pass container type constraint '$container_type_constraint'"
92 CORE::push @{ $reader->($instance) } => @_;
97 my $instance = CORE::shift;
98 CORE::push @{ $reader->($instance) } => @_;
104 my ( $attr, $reader, $writer ) = @_;
106 CORE::pop @{ $reader->( $_[0] ) };
110 sub unshift : method {
111 my ( $attr, $reader, $writer ) = @_;
113 $attr->has_type_constraint
114 && $attr->type_constraint->isa(
115 'Moose::Meta::TypeConstraint::Parameterized')
118 my $container_type_constraint = $attr->type_constraint->type_parameter;
120 my $instance = CORE::shift;
121 $container_type_constraint->check($_)
124 . " did not pass container type constraint '$container_type_constraint'"
126 CORE::unshift @{ $reader->($instance) } => @_;
131 my $instance = CORE::shift;
132 CORE::unshift @{ $reader->($instance) } => @_;
138 my ( $attr, $reader, $writer ) = @_;
140 CORE::shift @{ $reader->( $_[0] ) };
145 my ( $attr, $reader, $writer ) = @_;
147 $reader->( $_[0] )->[ $_[1] ];
152 my ( $attr, $reader, $writer ) = @_;
154 $attr->has_type_constraint
155 && $attr->type_constraint->isa(
156 'Moose::Meta::TypeConstraint::Parameterized')
159 my $container_type_constraint = $attr->type_constraint->type_parameter;
161 ( $container_type_constraint->check( $_[2] ) )
163 . ( $_[2] || 'undef' )
164 . " did not pass container type constraint '$container_type_constraint'";
165 $reader->( $_[0] )->[ $_[1] ] = $_[2];
170 $reader->( $_[0] )->[ $_[1] ] = $_[2];
175 sub accessor : method {
176 my ( $attr, $reader, $writer ) = @_;
179 $attr->has_type_constraint
180 && $attr->type_constraint->isa(
181 'Moose::Meta::TypeConstraint::Parameterized')
184 my $container_type_constraint = $attr->type_constraint->type_parameter;
188 if ( @_ == 1 ) { # reader
189 return $reader->($self)->[ $_[0] ];
191 elsif ( @_ == 2 ) { # writer
192 ( $container_type_constraint->check( $_[1] ) )
194 . ( $_[1] || 'undef' )
195 . " did not pass container type constraint '$container_type_constraint'";
196 $reader->($self)->[ $_[0] ] = $_[1];
199 confess "One or two arguments expected, not " . @_;
207 if ( @_ == 1 ) { # reader
208 return $reader->($self)->[ $_[0] ];
210 elsif ( @_ == 2 ) { # writer
211 $reader->($self)->[ $_[0] ] = $_[1];
214 confess "One or two arguments expected, not " . @_;
221 my ( $attr, $reader, $writer ) = @_;
223 @{ $reader->( $_[0] ) } = ();
227 sub delete : method {
228 my ( $attr, $reader, $writer ) = @_;
230 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 1;
234 sub insert : method {
235 my ( $attr, $reader, $writer ) = @_;
237 $attr->has_type_constraint
238 && $attr->type_constraint->isa(
239 'Moose::Meta::TypeConstraint::Parameterized')
242 my $container_type_constraint = $attr->type_constraint->type_parameter;
244 ( $container_type_constraint->check( $_[2] ) )
246 . ( $_[2] || 'undef' )
247 . " did not pass container type constraint '$container_type_constraint'";
248 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
253 CORE::splice @{ $reader->( $_[0] ) }, $_[1], 0, $_[2];
258 sub splice : method {
259 my ( $attr, $reader, $writer ) = @_;
261 $attr->has_type_constraint
262 && $attr->type_constraint->isa(
263 'Moose::Meta::TypeConstraint::Parameterized')
266 my $container_type_constraint = $attr->type_constraint->type_parameter;
268 my ( $self, $i, $j, @elems ) = @_;
269 ( $container_type_constraint->check($_) )
271 . ( defined($_) ? $_ : 'undef' )
272 . " did not pass container type constraint '$container_type_constraint'"
274 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
279 my ( $self, $i, $j, @elems ) = @_;
280 CORE::splice @{ $reader->($self) }, $i, $j, @elems;
285 sub sort_in_place : method {
286 my ( $attr, $reader, $writer ) = @_;
288 my ( $instance, $predicate ) = @_;
290 die "Argument must be a code reference"
291 if $predicate && ref $predicate ne 'CODE';
296 CORE::sort { $predicate->( $a, $b ) } @{ $reader->($instance) };
299 @sorted = CORE::sort @{ $reader->($instance) };
302 $writer->( $instance, \@sorted );
306 sub natatime : method {
307 my ( $attr, $reader, $writer ) = @_;
309 my ( $instance, $n, $f ) = @_;
310 my $it = List::MoreUtils::natatime($n, @{ $reader->($instance) });
311 return $it unless $f;
313 while (my @vals = $it->()) {
329 Moose::Meta::Attribute::Native::MethodProvider::Array - role providing method generators for Array trait
333 This is a role which provides the method generators for
334 L<Moose::Meta::Attribute::Native::Trait::Array>. Please check there for
335 documentation on what methods are provided.
347 See L<Moose/BUGS> for details on reporting bugs.
351 Stevan Little E<lt>stevan@iinteractive.comE<gt>
353 =head1 COPYRIGHT AND LICENSE
355 Copyright 2007-2009 by Infinity Interactive, Inc.
357 L<http://www.iinteractive.com>
359 This library is free software; you can redistribute it and/or modify
360 it under the same terms as Perl itself.