Commit | Line | Data |
b9dc8e2f |
1 | package MooseX::AttributeHelpers::MethodProvider::Array; |
2 | use Moose::Role; |
720fa35b |
3 | use MooseX::AttributeHelpers::Collection::TypeCheck; |
b9dc8e2f |
4 | |
9a976497 |
5 | our $VERSION = '0.05'; |
457dc4fb |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | with 'MooseX::AttributeHelpers::MethodProvider::List'; |
9 | |
b9dc8e2f |
10 | sub push : method { |
457dc4fb |
11 | my ($attr, $reader, $writer) = @_; |
720fa35b |
12 | return type_check($attr, sub {@_[1,$#_]}, sub { |
13 | my $self = shift; |
14 | CORE::push(@{ $reader->($self) }, @_); |
15 | }); |
b9dc8e2f |
16 | } |
17 | |
18 | sub pop : method { |
457dc4fb |
19 | my ($attr, $reader, $writer) = @_; |
720fa35b |
20 | return sub { CORE::pop(@{ $reader->($_[0]) }) }; |
b9dc8e2f |
21 | } |
22 | |
23 | sub unshift : method { |
457dc4fb |
24 | my ($attr, $reader, $writer) = @_; |
720fa35b |
25 | return type_check($attr, sub {@_[1,$#_]}, sub { |
26 | my $self = shift; |
27 | CORE::unshift(@{ $reader->($self) }, @_); |
28 | }); |
b9dc8e2f |
29 | } |
30 | |
31 | sub shift : method { |
457dc4fb |
32 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
33 | return sub { |
720fa35b |
34 | CORE::shift(@{ $reader->($_[0]) }); |
b9dc8e2f |
35 | }; |
36 | } |
37 | |
38 | sub get : method { |
457dc4fb |
39 | my ($attr, $reader, $writer) = @_; |
b9dc8e2f |
40 | return sub { |
720fa35b |
41 | my $self = shift; |
42 | return @{ $reader->($self) }[@_]; |
b9dc8e2f |
43 | }; |
44 | } |
45 | |
46 | sub set : method { |
457dc4fb |
47 | my ($attr, $reader, $writer) = @_; |
720fa35b |
48 | return type_check($attr, sub {@_[2,$#_]}, sub { |
49 | my ($self, $index, @values) = @_; |
50 | my @indexes = (ref $index eq 'ARRAY' ? @$index : ($index)); |
51 | @{ $reader->($self) }[@indexes] = @values; |
52 | }); |
b9dc8e2f |
53 | } |
b91f57af |
54 | |
55 | sub clear : method { |
56 | my ($attr, $reader, $writer) = @_; |
720fa35b |
57 | return sub { @{ $reader->($_[0]) } = () }; |
b91f57af |
58 | } |
59 | |
60 | sub delete : method { |
61 | my ($attr, $reader, $writer) = @_; |
62 | return sub { |
720fa35b |
63 | CORE::splice(@{ $reader->($_[0]) }, $_[1], $_[2] || 1); |
64 | }; |
b91f57af |
65 | } |
66 | |
67 | sub insert : method { |
68 | my ($attr, $reader, $writer) = @_; |
720fa35b |
69 | return type_contraint($attr, sub {@_[2,$#_]}, sub { |
70 | my ($self, $index, @values) = @_; |
71 | CORE::splice(@{ $reader->($self) }, $index, 0, @values); |
72 | }); |
b91f57af |
73 | } |
b9dc8e2f |
74 | |
b9dc8e2f |
75 | 1; |
76 | |
77 | __END__ |
78 | |
79 | =pod |
80 | |
5431dff2 |
81 | =head1 NAME |
82 | |
83 | MooseX::AttributeHelpers::MethodProvider::Array |
84 | |
85 | =head1 DESCRIPTION |
86 | |
87 | This is a role which provides the method generators for |
88 | L<MooseX::AttributeHelpers::Collection::Array>. |
89 | |
720fa35b |
90 | =head1 PROVIDED METHODS |
91 | |
92 | This module consumes L<MooseX::AttributeHelpers::MethodProvider::List>, and so |
93 | provides all of its methods as well. All methods work when multiple indexes |
94 | are supplied - special cases are noted. |
5431dff2 |
95 | |
96 | =over 4 |
97 | |
720fa35b |
98 | =item B<get(@indexes)> |
5431dff2 |
99 | |
720fa35b |
100 | Behaves just like indexing an arrayref: returns the items indexed by the |
101 | supplied arguments (i.e. C<$self->get_my_stuff(1,2,3)> means |
102 | C<@{$aref}[1,2,3]>). |
5431dff2 |
103 | |
720fa35b |
104 | =item B<set($index, $value)> |
5431dff2 |
105 | |
720fa35b |
106 | =item B<set([$indexes], @values)> |
5431dff2 |
107 | |
720fa35b |
108 | This is just like assigning to an arrayref, except that an arrayref lets you |
109 | assign multiple indexes at once with no strange syntax. You can do that with |
110 | this set as well, but the first argument should be an arrayref of the keys you |
111 | want to assign to. (e.g. C<$self->set_aref([1,2,3], qw(foo bar baz))>) |
5431dff2 |
112 | |
5431dff2 |
113 | =item B<pop> |
114 | |
720fa35b |
115 | L<perlfunc/pop> |
5431dff2 |
116 | |
720fa35b |
117 | =item B<push($item)> |
118 | |
119 | L<perlfunc/push> |
5431dff2 |
120 | |
121 | =item B<shift> |
122 | |
720fa35b |
123 | L<perlfunc/shift> |
124 | |
125 | =item B<unshift($item)> |
126 | |
127 | L<perlfunc/unshift> |
5431dff2 |
128 | |
8cf40f80 |
129 | =item B<clear> |
130 | |
720fa35b |
131 | Deletes all items from the array. |
132 | |
133 | =item B<delete($index, $length)> |
134 | |
135 | Deletes $length (default: 1) items from the array at $index. |
136 | |
137 | =item B<insert($index, @items)> |
b91f57af |
138 | |
720fa35b |
139 | Inserts @items into list at $index. |
b91f57af |
140 | |
5431dff2 |
141 | =back |
142 | |
143 | =head1 BUGS |
144 | |
145 | All complex software has bugs lurking in it, and this module is no |
146 | exception. If you find a bug please either email me, or add the bug |
147 | to cpan-RT. |
148 | |
149 | =head1 AUTHOR |
150 | |
151 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
152 | |
153 | =head1 COPYRIGHT AND LICENSE |
154 | |
99c62fb8 |
155 | Copyright 2007-2008 by Infinity Interactive, Inc. |
5431dff2 |
156 | |
157 | L<http://www.iinteractive.com> |
158 | |
159 | This library is free software; you can redistribute it and/or modify |
160 | it under the same terms as Perl itself. |
161 | |
b9dc8e2f |
162 | =cut |