small fix in docs
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
65a43f48 1package MooseX::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
3
457dc4fb 4our $VERSION = '0.01';
5our $AUTHORITY = 'cpan:STEVAN';
6
65a43f48 7sub exists : method {
457dc4fb 8 my ($attr, $reader, $writer) = @_;
9 return sub { exists $reader->($_[0])->{$_[1]} ? 1 : 0 };
65a43f48 10}
11
12sub get : method {
457dc4fb 13 my ($attr, $reader, $writer) = @_;
14 return sub { $reader->($_[0])->{$_[1]} };
65a43f48 15}
16
17sub set : method {
457dc4fb 18 my ($attr, $reader, $writer) = @_;
65a43f48 19 if ($attr->has_container_type) {
20 my $container_type_constraint = $attr->container_type_constraint;
21 return sub {
22 ($container_type_constraint->check($_[2]))
23 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint";
457dc4fb 24 $reader->($_[0])->{$_[1]} = $_[2]
65a43f48 25 };
26 }
27 else {
457dc4fb 28 return sub { $reader->($_[0])->{$_[1]} = $_[2] };
65a43f48 29 }
30}
31
32sub keys : method {
457dc4fb 33 my ($attr, $reader, $writer) = @_;
34 return sub { keys %{$reader->($_[0])} };
65a43f48 35}
36
37sub values : method {
457dc4fb 38 my ($attr, $reader, $writer) = @_;
39 return sub { values %{$reader->($_[0])} };
65a43f48 40}
41
42sub count : method {
457dc4fb 43 my ($attr, $reader, $writer) = @_;
44 return sub { scalar keys %{$reader->($_[0])} };
65a43f48 45}
46
47sub empty : method {
457dc4fb 48 my ($attr, $reader, $writer) = @_;
49 return sub { scalar keys %{$reader->($_[0])} ? 1 : 0 };
65a43f48 50}
51
52sub delete : method {
457dc4fb 53 my ($attr, $reader, $writer) = @_;
54 return sub { delete $reader->($_[0])->{$_[1]} };
65a43f48 55}
56
571;
58
5431dff2 59__END__
60
61=pod
62
63=head1 NAME
64
65MooseX::AttributeHelpers::MethodProvider::Hash
66
67=head1 DESCRIPTION
68
69This is a role which provides the method generators for
70L<MooseX::AttributeHelpers::Collection::Hash>.
71
72=head1 METHODS
73
74=over 4
75
76=item B<meta>
77
78=back
79
80=head1 PROVIDED METHODS
81
82=over 4
83
84=item B<count>
85
86=item B<delete>
87
88=item B<empty>
89
90=item B<exists>
91
92=item B<get>
93
94=item B<keys>
95
96=item B<set>
97
98=item B<values>
99
100=back
101
102=head1 BUGS
103
104All complex software has bugs lurking in it, and this module is no
105exception. If you find a bug please either email me, or add the bug
106to cpan-RT.
107
108=head1 AUTHOR
109
110Stevan Little E<lt>stevan@iinteractive.comE<gt>
111
112=head1 COPYRIGHT AND LICENSE
113
114Copyright 2007 by Infinity Interactive, Inc.
115
116L<http://www.iinteractive.com>
117
118This library is free software; you can redistribute it and/or modify
119it under the same terms as Perl itself.
120
121=cut
122