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