some POD advances
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
65a43f48 1package MooseX::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
3
4sub exists : method {
5 my ($attr) = @_;
6 return sub { exists $attr->get_value($_[0])->{$_[1]} ? 1 : 0 };
7}
8
9sub get : method {
10 my ($attr) = @_;
11 return sub { $attr->get_value($_[0])->{$_[1]} };
12}
13
14sub set : method {
15 my ($attr) = @_;
16 if ($attr->has_container_type) {
17 my $container_type_constraint = $attr->container_type_constraint;
18 return sub {
19 ($container_type_constraint->check($_[2]))
20 || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint";
21 $attr->get_value($_[0])->{$_[1]} = $_[2]
22 };
23 }
24 else {
25 return sub { $attr->get_value($_[0])->{$_[1]} = $_[2] };
26 }
27}
28
29sub keys : method {
30 my ($attr) = @_;
31 return sub { keys %{$attr->get_value($_[0])} };
32}
33
34sub values : method {
35 my ($attr) = @_;
36 return sub { values %{$attr->get_value($_[0])} };
37}
38
39sub count : method {
40 my ($attr) = @_;
41 return sub { scalar keys %{$attr->get_value($_[0])} };
42}
43
44sub empty : method {
45 my ($attr) = @_;
46 return sub { scalar keys %{$attr->get_value($_[0])} ? 1 : 0 };
47}
48
49sub delete : method {
50 my ($attr) = @_;
51 return sub { delete $attr->get_value($_[0])->{$_[1]} };
52}
53
541;
55
56__END__