more tests and tweaks
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Collection / Hash.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Collection::Hash;
d26633fc 3use Moose;
22d869ff 4
5our $VERSION = '0.01';
6our $AUTHORITY = 'cpan:STEVAN';
7
8c651099 8extends 'MooseX::AttributeHelpers::Collection';
d26633fc 9
8ba40fb0 10sub helper_type { 'HashRef' }
11
d26633fc 12has '+method_constructors' => (
13 default => sub {
14 return +{
15 'get' => sub {
16 my $attr = shift;
17 return sub { $attr->get_value($_[0])->{$_[1]} };
18 },
19 'set' => sub {
20 my $attr = shift;
8c651099 21 if ($attr->has_container_type) {
22 my $container_type_constraint = $attr->container_type_constraint;
23 return sub {
24 ($container_type_constraint->check($_[2]))
69dde336 25 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint";
8c651099 26 $attr->get_value($_[0])->{$_[1]} = $_[2]
27 };
28 }
29 else {
30 return sub { $attr->get_value($_[0])->{$_[1]} = $_[2] };
31 }
d26633fc 32 },
8c651099 33 'keys' => sub {
34 my $attr = shift;
35 return sub { keys %{$attr->get_value($_[0])} };
36 },
37 'values' => sub {
38 my $attr = shift;
39 return sub { values %{$attr->get_value($_[0])} };
40 },
d26633fc 41 'count' => sub {
42 my $attr = shift;
43 return sub { scalar keys %{$attr->get_value($_[0])} };
44 },
45 'empty' => sub {
46 my $attr = shift;
47 return sub { scalar keys %{$attr->get_value($_[0])} ? 1 : 0 };
48 }
49 }
50 }
51);
52
d26633fc 53no Moose;
54
55# register the alias ...
56package Moose::Meta::Attribute::Custom::Collection::Hash;
57sub register_implementation { 'MooseX::AttributeHelpers::Collection::Hash' }
58
59
22d869ff 601;
61
62__END__
63
64=pod
65
66=head1 NAME
67
68=head1 SYNOPSIS
69
70=head1 DESCRIPTION
71
72=head1 METHODS
73
74=head1 BUGS
75
76All complex software has bugs lurking in it, and this module is no
77exception. If you find a bug please either email me, or add the bug
78to cpan-RT.
79
80=head1 AUTHOR
81
82Stevan Little E<lt>stevan@iinteractive.comE<gt>
83
84=head1 COPYRIGHT AND LICENSE
85
86Copyright 2007 by Infinity Interactive, Inc.
87
88L<http://www.iinteractive.com>
89
90This library is free software; you can redistribute it and/or modify
91it under the same terms as Perl itself.
92
93=cut