Commit | Line | Data |
b9dc8e2f |
1 | package MooseX::AttributeHelpers::MethodProvider::Array; |
2 | use Moose::Role; |
3 | |
c91a1347 |
4 | our $VERSION = '0.02'; |
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 | |
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 | |
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) = @_; |
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 | |
57 | sub shift : method { |
457dc4fb |
58 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
59 | return sub { |
457dc4fb |
60 | CORE::shift @{$reader->($_[0])} |
b9dc8e2f |
61 | }; |
62 | } |
8cf40f80 |
63 | |
64 | sub clear : method { |
65 | my ($attr, $reader, $writer) = @_; |
66 | return sub { |
67 | @{$reader->($_[0])} = () |
68 | }; |
69 | } |
b9dc8e2f |
70 | |
71 | sub get : method { |
457dc4fb |
72 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
73 | return sub { |
457dc4fb |
74 | $reader->($_[0])->[$_[1]] |
b9dc8e2f |
75 | }; |
76 | } |
77 | |
78 | sub set : method { |
457dc4fb |
79 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
80 | if ($attr->has_container_type) { |
81 | my $container_type_constraint = $attr->container_type_constraint; |
82 | return sub { |
83 | ($container_type_constraint->check($_[2])) |
84 | || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; |
457dc4fb |
85 | $reader->($_[0])->[$_[1]] = $_[2] |
b9dc8e2f |
86 | }; |
87 | } |
88 | else { |
89 | return sub { |
457dc4fb |
90 | $reader->($_[0])->[$_[1]] = $_[2] |
b9dc8e2f |
91 | }; |
92 | } |
93 | } |
94 | |
b9dc8e2f |
95 | 1; |
96 | |
97 | __END__ |
98 | |
99 | =pod |
100 | |
5431dff2 |
101 | =head1 NAME |
102 | |
103 | MooseX::AttributeHelpers::MethodProvider::Array |
104 | |
105 | =head1 DESCRIPTION |
106 | |
107 | This is a role which provides the method generators for |
108 | L<MooseX::AttributeHelpers::Collection::Array>. |
109 | |
110 | =head1 METHODS |
111 | |
112 | =over 4 |
113 | |
114 | =item B<meta> |
115 | |
116 | =back |
117 | |
118 | =head1 PROVIDED METHODS |
119 | |
457dc4fb |
120 | This module also consumes the B<List> method providers, to |
121 | see those provied methods, refer to that documentation. |
5431dff2 |
122 | |
457dc4fb |
123 | =over 4 |
5431dff2 |
124 | |
125 | =item B<get> |
126 | |
5431dff2 |
127 | =item B<pop> |
128 | |
129 | =item B<push> |
130 | |
131 | =item B<set> |
132 | |
133 | =item B<shift> |
134 | |
135 | =item B<unshift> |
136 | |
8cf40f80 |
137 | =item B<clear> |
138 | |
5431dff2 |
139 | =back |
140 | |
141 | =head1 BUGS |
142 | |
143 | All complex software has bugs lurking in it, and this module is no |
144 | exception. If you find a bug please either email me, or add the bug |
145 | to cpan-RT. |
146 | |
147 | =head1 AUTHOR |
148 | |
149 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
150 | |
151 | =head1 COPYRIGHT AND LICENSE |
152 | |
153 | Copyright 2007 by Infinity Interactive, Inc. |
154 | |
155 | L<http://www.iinteractive.com> |
156 | |
157 | This library is free software; you can redistribute it and/or modify |
158 | it under the same terms as Perl itself. |
159 | |
b9dc8e2f |
160 | =cut |