Commit | Line | Data |
b9dc8e2f |
1 | package MooseX::AttributeHelpers::MethodProvider::Array; |
2 | use Moose::Role; |
3 | |
9a976497 |
4 | our $VERSION = '0.05'; |
457dc4fb |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
7 | with 'MooseX::AttributeHelpers::MethodProvider::List'; |
8 | |
b9dc8e2f |
9 | sub push : method { |
457dc4fb |
10 | my ($attr, $reader, $writer) = @_; |
11 | |
9a976497 |
12 | if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { |
13 | my $container_type_constraint = $attr->type_constraint->type_parameter; |
b9dc8e2f |
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 | |
30 | sub pop : method { |
457dc4fb |
31 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
32 | return sub { |
457dc4fb |
33 | CORE::pop @{$reader->($_[0])} |
b9dc8e2f |
34 | }; |
35 | } |
36 | |
37 | sub unshift : method { |
457dc4fb |
38 | my ($attr, $reader, $writer) = @_; |
9a976497 |
39 | if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { |
40 | my $container_type_constraint = $attr->type_constraint->type_parameter; |
b9dc8e2f |
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 | |
57 | sub shift : method { |
457dc4fb |
58 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
59 | return sub { |
457dc4fb |
60 | CORE::shift @{$reader->($_[0])} |
b9dc8e2f |
61 | }; |
62 | } |
63 | |
64 | sub get : method { |
457dc4fb |
65 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
66 | return sub { |
457dc4fb |
67 | $reader->($_[0])->[$_[1]] |
b9dc8e2f |
68 | }; |
69 | } |
70 | |
71 | sub set : method { |
457dc4fb |
72 | my ($attr, $reader, $writer) = @_; |
9a976497 |
73 | if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { |
74 | my $container_type_constraint = $attr->type_constraint->type_parameter; |
b9dc8e2f |
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 | } |
b91f57af |
87 | |
88 | sub clear : method { |
89 | my ($attr, $reader, $writer) = @_; |
90 | return sub { |
91 | @{$reader->($_[0])} = () |
92 | }; |
93 | } |
94 | |
95 | sub delete : method { |
96 | my ($attr, $reader, $writer) = @_; |
97 | return sub { |
98 | CORE::splice @{$reader->($_[0])}, $_[1], 1; |
99 | } |
100 | } |
101 | |
102 | sub insert : method { |
103 | my ($attr, $reader, $writer) = @_; |
9a976497 |
104 | if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { |
105 | my $container_type_constraint = $attr->type_constraint->type_parameter; |
b91f57af |
106 | return sub { |
107 | ($container_type_constraint->check($_[2])) |
108 | || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; |
9a976497 |
109 | CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2]; |
b91f57af |
110 | }; |
111 | } |
112 | else { |
113 | return sub { |
9a976497 |
114 | CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2]; |
b91f57af |
115 | }; |
116 | } |
117 | } |
b9dc8e2f |
118 | |
b9dc8e2f |
119 | 1; |
120 | |
121 | __END__ |
122 | |
123 | =pod |
124 | |
5431dff2 |
125 | =head1 NAME |
126 | |
127 | MooseX::AttributeHelpers::MethodProvider::Array |
128 | |
129 | =head1 DESCRIPTION |
130 | |
131 | This is a role which provides the method generators for |
132 | L<MooseX::AttributeHelpers::Collection::Array>. |
133 | |
134 | =head1 METHODS |
135 | |
136 | =over 4 |
137 | |
138 | =item B<meta> |
139 | |
140 | =back |
141 | |
142 | =head1 PROVIDED METHODS |
143 | |
457dc4fb |
144 | This module also consumes the B<List> method providers, to |
145 | see those provied methods, refer to that documentation. |
5431dff2 |
146 | |
457dc4fb |
147 | =over 4 |
5431dff2 |
148 | |
149 | =item B<get> |
150 | |
5431dff2 |
151 | =item B<pop> |
152 | |
153 | =item B<push> |
154 | |
155 | =item B<set> |
156 | |
157 | =item B<shift> |
158 | |
159 | =item B<unshift> |
160 | |
8cf40f80 |
161 | =item B<clear> |
162 | |
b91f57af |
163 | =item B<delete> |
164 | |
165 | =item B<insert> |
166 | |
5431dff2 |
167 | =back |
168 | |
169 | =head1 BUGS |
170 | |
171 | All complex software has bugs lurking in it, and this module is no |
172 | exception. If you find a bug please either email me, or add the bug |
173 | to cpan-RT. |
174 | |
175 | =head1 AUTHOR |
176 | |
177 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
178 | |
179 | =head1 COPYRIGHT AND LICENSE |
180 | |
99c62fb8 |
181 | Copyright 2007-2008 by Infinity Interactive, Inc. |
5431dff2 |
182 | |
183 | L<http://www.iinteractive.com> |
184 | |
185 | This library is free software; you can redistribute it and/or modify |
186 | it under the same terms as Perl itself. |
187 | |
b9dc8e2f |
188 | =cut |