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