Commit | Line | Data |
22d869ff |
1 | |
2 | package MooseX::AttributeHelpers::Collection::Array; |
3 | use Moose; |
22d869ff |
4 | |
5 | our $VERSION = '0.01'; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
d26633fc |
8 | extends 'MooseX::AttributeHelpers::Base'; |
9 | |
10 | has '+method_constructors' => ( |
11 | default => sub { |
12 | return +{ |
13 | 'push' => sub { |
14 | my $attr = shift; |
15 | return sub { |
16 | my $instance = shift; |
17 | push @{$attr->get_value($instance)} => @_; |
18 | }; |
19 | }, |
20 | 'pop' => sub { |
21 | my $attr = shift; |
22 | return sub { pop @{$attr->get_value($_[0])} }; |
23 | }, |
24 | 'unshift' => sub { |
25 | my $attr = shift; |
26 | return sub { |
27 | my $instance = shift; |
28 | unshift @{$attr->get_value($instance)} => @_; |
29 | }; |
30 | }, |
31 | 'shift' => sub { |
32 | my $attr = shift; |
33 | return sub { shift @{$attr->get_value($_[0])} }; |
34 | }, |
35 | 'get' => sub { |
36 | my $attr = shift; |
37 | return sub { $attr->get_value($_[0])->[$_[1]] }; |
38 | }, |
39 | 'set' => sub { |
40 | my $attr = shift; |
41 | return sub { $attr->get_value($_[0])->[$_[1]] = $_[2] }; |
42 | }, |
43 | 'count' => sub { |
44 | my $attr = shift; |
45 | return sub { scalar @{$attr->get_value($_[0])} }; |
46 | }, |
47 | 'empty' => sub { |
48 | my $attr = shift; |
49 | return sub { scalar @{$attr->get_value($_[0])} ? 1 : 0 }; |
50 | } |
51 | } |
52 | } |
22d869ff |
53 | ); |
54 | |
d26633fc |
55 | sub _process_options_for_provides { |
56 | my ($self, $options) = @_; |
57 | (exists $options->{isa}) |
58 | || confess "You must define a type with the Array metaclass"; |
59 | |
60 | (find_type_constraint($options->{isa})->is_subtype_of('ArrayRef')) |
61 | || confess "The type constraint for a Array must be a subtype of ArrayRef"; |
62 | } |
22d869ff |
63 | |
64 | no Moose; |
22d869ff |
65 | |
66 | # register the alias ... |
d26633fc |
67 | package Moose::Meta::Attribute::Custom::Collection::Array; |
22d869ff |
68 | sub register_implementation { 'MooseX::AttributeHelpers::Collection::Array' } |
69 | |
70 | |
71 | 1; |
72 | |
73 | __END__ |
74 | |
75 | =pod |
76 | |
77 | =head1 NAME |
78 | |
79 | =head1 SYNOPSIS |
80 | |
81 | package Stuff; |
82 | use Moose; |
83 | |
84 | has 'options' => ( |
85 | metaclass => 'Collection', |
86 | is => 'ro', |
87 | isa => 'ArrayRef', |
88 | default => sub { [] }, |
89 | provides => { |
90 | 'push' => 'add_options', |
91 | 'pop' => 'remove_last_option', |
92 | } |
93 | ); |
94 | |
95 | =head1 DESCRIPTION |
96 | |
97 | =head1 METHODS |
98 | |
99 | =head1 BUGS |
100 | |
101 | All complex software has bugs lurking in it, and this module is no |
102 | exception. If you find a bug please either email me, or add the bug |
103 | to cpan-RT. |
104 | |
105 | =head1 AUTHOR |
106 | |
107 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
108 | |
109 | =head1 COPYRIGHT AND LICENSE |
110 | |
111 | Copyright 2007 by Infinity Interactive, Inc. |
112 | |
113 | L<http://www.iinteractive.com> |
114 | |
115 | This library is free software; you can redistribute it and/or modify |
116 | it under the same terms as Perl itself. |
117 | |
118 | =cut |