more tweaks, I think I want to make this into a role
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Collection / Array.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Collection::Array;
3use Moose;
22d869ff 4
5our $VERSION = '0.01';
6our $AUTHORITY = 'cpan:STEVAN';
7
d26633fc 8extends 'MooseX::AttributeHelpers::Base';
9
10has '+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 55sub _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
64no Moose;
22d869ff 65
66# register the alias ...
d26633fc 67package Moose::Meta::Attribute::Custom::Collection::Array;
22d869ff 68sub register_implementation { 'MooseX::AttributeHelpers::Collection::Array' }
69
70
711;
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
101All complex software has bugs lurking in it, and this module is no
102exception. If you find a bug please either email me, or add the bug
103to cpan-RT.
104
105=head1 AUTHOR
106
107Stevan Little E<lt>stevan@iinteractive.comE<gt>
108
109=head1 COPYRIGHT AND LICENSE
110
111Copyright 2007 by Infinity Interactive, Inc.
112
113L<http://www.iinteractive.com>
114
115This library is free software; you can redistribute it and/or modify
116it under the same terms as Perl itself.
117
118=cut