small fix in docs
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Array.pm
CommitLineData
b9dc8e2f 1package MooseX::AttributeHelpers::MethodProvider::Array;
2use Moose::Role;
3
c91a1347 4our $VERSION = '0.02';
457dc4fb 5our $AUTHORITY = 'cpan:STEVAN';
6
7with 'MooseX::AttributeHelpers::MethodProvider::List';
8
b9dc8e2f 9sub push : method {
457dc4fb 10 my ($attr, $reader, $writer) = @_;
11
b9dc8e2f 12 if ($attr->has_container_type) {
13 my $container_type_constraint = $attr->container_type_constraint;
14 return sub {
15 my $instance = CORE::shift;
16 $container_type_constraint->check($_)
17 || confess "Value " . ($_||'undef') . " did not pass container type constraint"
18 foreach @_;
457dc4fb 19 CORE::push @{$reader->($instance)} => @_;
b9dc8e2f 20 };
21 }
22 else {
23 return sub {
24 my $instance = CORE::shift;
457dc4fb 25 CORE::push @{$reader->($instance)} => @_;
b9dc8e2f 26 };
27 }
28}
29
30sub pop : method {
457dc4fb 31 my ($attr, $reader, $writer) = @_;
b9dc8e2f 32 return sub {
457dc4fb 33 CORE::pop @{$reader->($_[0])}
b9dc8e2f 34 };
35}
36
37sub unshift : method {
457dc4fb 38 my ($attr, $reader, $writer) = @_;
b9dc8e2f 39 if ($attr->has_container_type) {
40 my $container_type_constraint = $attr->container_type_constraint;
41 return sub {
42 my $instance = CORE::shift;
43 $container_type_constraint->check($_)
44 || confess "Value " . ($_||'undef') . " did not pass container type constraint"
45 foreach @_;
457dc4fb 46 CORE::unshift @{$reader->($instance)} => @_;
b9dc8e2f 47 };
48 }
49 else {
50 return sub {
51 my $instance = CORE::shift;
457dc4fb 52 CORE::unshift @{$reader->($instance)} => @_;
b9dc8e2f 53 };
54 }
55}
56
57sub shift : method {
457dc4fb 58 my ($attr, $reader, $writer) = @_;
b9dc8e2f 59 return sub {
457dc4fb 60 CORE::shift @{$reader->($_[0])}
b9dc8e2f 61 };
62}
63
64sub get : method {
457dc4fb 65 my ($attr, $reader, $writer) = @_;
b9dc8e2f 66 return sub {
457dc4fb 67 $reader->($_[0])->[$_[1]]
b9dc8e2f 68 };
69}
70
71sub set : method {
457dc4fb 72 my ($attr, $reader, $writer) = @_;
b9dc8e2f 73 if ($attr->has_container_type) {
74 my $container_type_constraint = $attr->container_type_constraint;
75 return sub {
76 ($container_type_constraint->check($_[2]))
77 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint";
457dc4fb 78 $reader->($_[0])->[$_[1]] = $_[2]
b9dc8e2f 79 };
80 }
81 else {
82 return sub {
457dc4fb 83 $reader->($_[0])->[$_[1]] = $_[2]
b9dc8e2f 84 };
85 }
86}
87
b9dc8e2f 881;
89
90__END__
91
92=pod
93
5431dff2 94=head1 NAME
95
96MooseX::AttributeHelpers::MethodProvider::Array
97
98=head1 DESCRIPTION
99
100This is a role which provides the method generators for
101L<MooseX::AttributeHelpers::Collection::Array>.
102
103=head1 METHODS
104
105=over 4
106
107=item B<meta>
108
109=back
110
111=head1 PROVIDED METHODS
112
457dc4fb 113This module also consumes the B<List> method providers, to
114see those provied methods, refer to that documentation.
5431dff2 115
457dc4fb 116=over 4
5431dff2 117
118=item B<get>
119
5431dff2 120=item B<pop>
121
122=item B<push>
123
124=item B<set>
125
126=item B<shift>
127
128=item B<unshift>
129
130=back
131
132=head1 BUGS
133
134All complex software has bugs lurking in it, and this module is no
135exception. If you find a bug please either email me, or add the bug
136to cpan-RT.
137
138=head1 AUTHOR
139
140Stevan Little E<lt>stevan@iinteractive.comE<gt>
141
142=head1 COPYRIGHT AND LICENSE
143
144Copyright 2007 by Infinity Interactive, Inc.
145
146L<http://www.iinteractive.com>
147
148This library is free software; you can redistribute it and/or modify
149it under the same terms as Perl itself.
150
b9dc8e2f 151=cut