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