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"; |
60 | |
8a9cea9b |
61 | my $c = find_type_constraint($options->{isa}) || $options->{isa}; |
62 | ( $c && blessed($c) && $c->is_a_type_of('ArrayRef')) |
88aaf2bd |
63 | || confess "The type constraint for a Array ($options->{isa}) must be a subtype of ArrayRef"; |
d26633fc |
64 | } |
22d869ff |
65 | |
66 | no Moose; |
88aaf2bd |
67 | no Moose::Util::TypeConstraints;; |
22d869ff |
68 | |
69 | # register the alias ... |
d26633fc |
70 | package Moose::Meta::Attribute::Custom::Collection::Array; |
22d869ff |
71 | sub register_implementation { 'MooseX::AttributeHelpers::Collection::Array' } |
72 | |
73 | |
74 | 1; |
75 | |
76 | __END__ |
77 | |
78 | =pod |
79 | |
80 | =head1 NAME |
81 | |
82 | =head1 SYNOPSIS |
83 | |
84 | package Stuff; |
85 | use Moose; |
86 | |
87 | has 'options' => ( |
88 | metaclass => 'Collection', |
89 | is => 'ro', |
90 | isa => 'ArrayRef', |
91 | default => sub { [] }, |
92 | provides => { |
93 | 'push' => 'add_options', |
94 | 'pop' => 'remove_last_option', |
95 | } |
96 | ); |
97 | |
98 | =head1 DESCRIPTION |
99 | |
100 | =head1 METHODS |
101 | |
102 | =head1 BUGS |
103 | |
104 | All complex software has bugs lurking in it, and this module is no |
105 | exception. If you find a bug please either email me, or add the bug |
106 | to cpan-RT. |
107 | |
108 | =head1 AUTHOR |
109 | |
110 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
111 | |
112 | =head1 COPYRIGHT AND LICENSE |
113 | |
114 | Copyright 2007 by Infinity Interactive, Inc. |
115 | |
116 | L<http://www.iinteractive.com> |
117 | |
118 | This library is free software; you can redistribute it and/or modify |
119 | it under the same terms as Perl itself. |
120 | |
8a9cea9b |
121 | =cut |