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) = @_;
13 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
14 my $container_type_constraint = $attr->type_constraint->type_parameter;
16 my $instance = CORE::shift;
17 $container_type_constraint->check($_)
18 || confess "Value " . ($_||'undef') . " did not pass container type constraint '$container_type_constraint'"
20 CORE::push @{$reader->($instance)} => @_;
25 my $instance = CORE::shift;
26 CORE::push @{$reader->($instance)} => @_;
32 my ($attr, $reader, $writer) = @_;
34 CORE::pop @{$reader->($_[0])}
38 sub unshift : method {
39 my ($attr, $reader, $writer) = @_;
40 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
41 my $container_type_constraint = $attr->type_constraint->type_parameter;
43 my $instance = CORE::shift;
44 $container_type_constraint->check($_)
45 || confess "Value " . ($_||'undef') . " did not pass container type constraint '$container_type_constraint'"
47 CORE::unshift @{$reader->($instance)} => @_;
52 my $instance = CORE::shift;
53 CORE::unshift @{$reader->($instance)} => @_;
59 my ($attr, $reader, $writer) = @_;
61 CORE::shift @{$reader->($_[0])}
66 my ($attr, $reader, $writer) = @_;
68 $reader->($_[0])->[$_[1]]
73 my ($attr, $reader, $writer) = @_;
74 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
75 my $container_type_constraint = $attr->type_constraint->type_parameter;
77 ($container_type_constraint->check($_[2]))
78 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint '$container_type_constraint'";
79 $reader->($_[0])->[$_[1]] = $_[2]
84 $reader->($_[0])->[$_[1]] = $_[2]
89 sub accessor : method {
90 my ($attr, $reader, $writer) = @_;
92 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
93 my $container_type_constraint = $attr->type_constraint->type_parameter;
97 if (@_ == 1) { # reader
98 return $reader->($self)->[$_[0]];
100 elsif (@_ == 2) { # writer
101 ($container_type_constraint->check($_[1]))
102 || confess "Value " . ($_[1]||'undef') . " did not pass container type constraint '$container_type_constraint'";
103 $reader->($self)->[$_[0]] = $_[1];
106 confess "One or two arguments expected, not " . @_;
114 if (@_ == 1) { # reader
115 return $reader->($self)->[$_[0]];
117 elsif (@_ == 2) { # writer
118 $reader->($self)->[$_[0]] = $_[1];
121 confess "One or two arguments expected, not " . @_;
128 my ($attr, $reader, $writer) = @_;
130 @{$reader->($_[0])} = ()
134 sub delete : method {
135 my ($attr, $reader, $writer) = @_;
137 CORE::splice @{$reader->($_[0])}, $_[1], 1;
141 sub insert : method {
142 my ($attr, $reader, $writer) = @_;
143 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
144 my $container_type_constraint = $attr->type_constraint->type_parameter;
146 ($container_type_constraint->check($_[2]))
147 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint '$container_type_constraint'";
148 CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2];
153 CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2];
158 sub splice : method {
159 my ($attr, $reader, $writer) = @_;
160 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
161 my $container_type_constraint = $attr->type_constraint->type_parameter;
163 my ( $self, $i, $j, @elems ) = @_;
164 ($container_type_constraint->check($_))
165 || confess "Value " . (defined($_) ? $_ : 'undef') . " did not pass container type constraint '$container_type_constraint'" for @elems;
166 CORE::splice @{$reader->($self)}, $i, $j, @elems;
171 my ( $self, $i, $j, @elems ) = @_;
172 CORE::splice @{$reader->($self)}, $i, $j, @elems;
177 sub sort_in_place : method {
178 my ($attr, $reader, $writer) = @_;
180 my ($instance, $predicate) = @_;
182 die "Argument must be a code reference"
183 if $predicate && ref $predicate ne 'CODE';
187 @sorted = CORE::sort { $predicate->($a, $b) } @{$reader->($instance)};
190 @sorted = CORE::sort @{$reader->($instance)};
193 $writer->($instance, \@sorted);
205 Moose::AttributeHelpers::MethodProvider::Array
209 This is a role which provides the method generators for
210 L<Moose::AttributeHelpers::Collection::Array>.
220 =head1 PROVIDED METHODS
222 This module also consumes the B<List> method providers, to
223 see those provied methods, refer to that documentation.
247 =item B<sort_in_place>
249 Sorts the array I<in place>, modifying the value of the attribute.
251 You can provide an optional subroutine reference to sort with (as you
252 can with the core C<sort> function). However, instead of using C<$a>
253 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
257 If passed one argument, returns the value of the requested element.
258 If passed two arguments, sets the value of the requested element.
264 All complex software has bugs lurking in it, and this module is no
265 exception. If you find a bug please either email me, or add the bug
270 Stevan Little E<lt>stevan@iinteractive.comE<gt>
272 =head1 COPYRIGHT AND LICENSE
274 Copyright 2007-2009 by Infinity Interactive, Inc.
276 L<http://www.iinteractive.com>
278 This library is free software; you can redistribute it and/or modify
279 it under the same terms as Perl itself.