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