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