bump version to 0.22
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Array.pm
CommitLineData
b9dc8e2f 1package MooseX::AttributeHelpers::MethodProvider::Array;
2use Moose::Role;
3
3ebd23e6 4our $VERSION = '0.22';
38430345 5$VERSION = eval $VERSION;
457dc4fb 6our $AUTHORITY = 'cpan:STEVAN';
7
8with 'MooseX::AttributeHelpers::MethodProvider::List';
9
b9dc8e2f 10sub push : method {
457dc4fb 11 my ($attr, $reader, $writer) = @_;
12
9a976497 13 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
14 my $container_type_constraint = $attr->type_constraint->type_parameter;
b9dc8e2f 15 return sub {
16 my $instance = CORE::shift;
17 $container_type_constraint->check($_)
d071f896 18 || confess "Value " . ($_||'undef') . " did not pass container type constraint '$container_type_constraint'"
b9dc8e2f 19 foreach @_;
457dc4fb 20 CORE::push @{$reader->($instance)} => @_;
b9dc8e2f 21 };
22 }
23 else {
24 return sub {
25 my $instance = CORE::shift;
457dc4fb 26 CORE::push @{$reader->($instance)} => @_;
b9dc8e2f 27 };
28 }
29}
30
31sub pop : method {
457dc4fb 32 my ($attr, $reader, $writer) = @_;
b9dc8e2f 33 return sub {
457dc4fb 34 CORE::pop @{$reader->($_[0])}
b9dc8e2f 35 };
36}
37
38sub unshift : method {
457dc4fb 39 my ($attr, $reader, $writer) = @_;
9a976497 40 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
41 my $container_type_constraint = $attr->type_constraint->type_parameter;
b9dc8e2f 42 return sub {
43 my $instance = CORE::shift;
44 $container_type_constraint->check($_)
d071f896 45 || confess "Value " . ($_||'undef') . " did not pass container type constraint '$container_type_constraint'"
b9dc8e2f 46 foreach @_;
457dc4fb 47 CORE::unshift @{$reader->($instance)} => @_;
b9dc8e2f 48 };
49 }
50 else {
51 return sub {
52 my $instance = CORE::shift;
457dc4fb 53 CORE::unshift @{$reader->($instance)} => @_;
b9dc8e2f 54 };
55 }
56}
57
58sub shift : method {
457dc4fb 59 my ($attr, $reader, $writer) = @_;
b9dc8e2f 60 return sub {
457dc4fb 61 CORE::shift @{$reader->($_[0])}
b9dc8e2f 62 };
63}
64
65sub get : method {
457dc4fb 66 my ($attr, $reader, $writer) = @_;
b9dc8e2f 67 return sub {
457dc4fb 68 $reader->($_[0])->[$_[1]]
b9dc8e2f 69 };
70}
71
72sub set : method {
457dc4fb 73 my ($attr, $reader, $writer) = @_;
9a976497 74 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
75 my $container_type_constraint = $attr->type_constraint->type_parameter;
b9dc8e2f 76 return sub {
77 ($container_type_constraint->check($_[2]))
d071f896 78 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint '$container_type_constraint'";
457dc4fb 79 $reader->($_[0])->[$_[1]] = $_[2]
b9dc8e2f 80 };
81 }
82 else {
83 return sub {
457dc4fb 84 $reader->($_[0])->[$_[1]] = $_[2]
b9dc8e2f 85 };
86 }
87}
b91f57af 88
57e529ff 89sub accessor : method {
90 my ($attr, $reader, $writer) = @_;
91
92 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
93 my $container_type_constraint = $attr->type_constraint->type_parameter;
94 return sub {
95 my $self = shift;
96
97 if (@_ == 1) { # reader
98 return $reader->($self)->[$_[0]];
99 }
100 elsif (@_ == 2) { # writer
101 ($container_type_constraint->check($_[1]))
d071f896 102 || confess "Value " . ($_[1]||'undef') . " did not pass container type constraint '$container_type_constraint'";
57e529ff 103 $reader->($self)->[$_[0]] = $_[1];
104 }
105 else {
106 confess "One or two arguments expected, not " . @_;
107 }
108 };
109 }
110 else {
111 return sub {
112 my $self = shift;
113
114 if (@_ == 1) { # reader
115 return $reader->($self)->[$_[0]];
116 }
117 elsif (@_ == 2) { # writer
118 $reader->($self)->[$_[0]] = $_[1];
119 }
120 else {
121 confess "One or two arguments expected, not " . @_;
122 }
123 };
124 }
125}
126
b91f57af 127sub clear : method {
128 my ($attr, $reader, $writer) = @_;
129 return sub {
130 @{$reader->($_[0])} = ()
131 };
132}
133
134sub delete : method {
135 my ($attr, $reader, $writer) = @_;
136 return sub {
137 CORE::splice @{$reader->($_[0])}, $_[1], 1;
138 }
139}
140
141sub insert : method {
142 my ($attr, $reader, $writer) = @_;
9a976497 143 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
144 my $container_type_constraint = $attr->type_constraint->type_parameter;
b91f57af 145 return sub {
146 ($container_type_constraint->check($_[2]))
d071f896 147 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint '$container_type_constraint'";
9a976497 148 CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2];
b91f57af 149 };
150 }
151 else {
152 return sub {
9a976497 153 CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2];
b91f57af 154 };
155 }
156}
331e1af0 157
158sub 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;
162 return sub {
163 my ( $self, $i, $j, @elems ) = @_;
164 ($container_type_constraint->check($_))
d071f896 165 || confess "Value " . (defined($_) ? $_ : 'undef') . " did not pass container type constraint '$container_type_constraint'" for @elems;
af1ade48 166 CORE::splice @{$reader->($self)}, $i, $j, @elems;
331e1af0 167 };
168 }
169 else {
170 return sub {
171 my ( $self, $i, $j, @elems ) = @_;
af1ade48 172 CORE::splice @{$reader->($self)}, $i, $j, @elems;
331e1af0 173 };
174 }
175}
176
80894c0a 177sub sort_in_place : method {
71703b28 178 my ($attr, $reader, $writer) = @_;
179 return sub {
180 my ($instance, $predicate) = @_;
181
182 die "Argument must be a code reference"
183 if $predicate && ref $predicate ne 'CODE';
184
185 my @sorted;
186 if ($predicate) {
187 @sorted = CORE::sort { $predicate->($a, $b) } @{$reader->($instance)};
188 }
189 else {
190 @sorted = CORE::sort @{$reader->($instance)};
191 }
192
193 $writer->($instance, \@sorted);
194 };
80894c0a 195}
196
b9dc8e2f 1971;
198
199__END__
200
201=pod
202
5431dff2 203=head1 NAME
204
205MooseX::AttributeHelpers::MethodProvider::Array
71703b28 206
5431dff2 207=head1 DESCRIPTION
208
209This is a role which provides the method generators for
210L<MooseX::AttributeHelpers::Collection::Array>.
211
212=head1 METHODS
213
214=over 4
215
216=item B<meta>
217
218=back
219
220=head1 PROVIDED METHODS
221
457dc4fb 222This module also consumes the B<List> method providers, to
223see those provied methods, refer to that documentation.
5431dff2 224
457dc4fb 225=over 4
5431dff2 226
227=item B<get>
228
5431dff2 229=item B<pop>
230
231=item B<push>
232
233=item B<set>
234
235=item B<shift>
236
237=item B<unshift>
238
8cf40f80 239=item B<clear>
240
b91f57af 241=item B<delete>
242
243=item B<insert>
244
331e1af0 245=item B<splice>
246
80894c0a 247=item B<sort_in_place>
71703b28 248
249Sorts the array I<in place>, modifying the value of the attribute.
250
251You can provide an optional subroutine reference to sort with (as you
252can with the core C<sort> function). However, instead of using C<$a>
253and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
80894c0a 254
57e529ff 255=item B<accessor>
256
257If passed one argument, returns the value of the requested element.
258If passed two arguments, sets the value of the requested element.
259
5431dff2 260=back
261
262=head1 BUGS
263
264All complex software has bugs lurking in it, and this module is no
265exception. If you find a bug please either email me, or add the bug
266to cpan-RT.
267
268=head1 AUTHOR
269
270Stevan Little E<lt>stevan@iinteractive.comE<gt>
271
272=head1 COPYRIGHT AND LICENSE
273
9c5d164e 274Copyright 2007-2009 by Infinity Interactive, Inc.
5431dff2 275
276L<http://www.iinteractive.com>
277
278This library is free software; you can redistribute it and/or modify
279it under the same terms as Perl itself.
280
b9dc8e2f 281=cut