Add future ideas file
[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             'delete' => sub {
54                 my $attr = shift;
55                 return sub { delete $attr->get_value($_[0])->{$_[1]} };
56             }
57         }
58     }
59 );
60
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
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