Added test for "kv", added new method "elements" to MX::AH::MP::ImmutableHash
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / ImmutableHash.pm
CommitLineData
9a976497 1package MooseX::AttributeHelpers::MethodProvider::ImmutableHash;
2use Moose::Role;
3
4a0da5ad 4our $VERSION = '0.19';
38430345 5$VERSION = eval $VERSION;
9a976497 6our $AUTHORITY = 'cpan:STEVAN';
7
8sub exists : method {
56803bef 9 my ($attr, $reader, $writer) = @_;
9a976497 10 return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 };
56803bef 11}
9a976497 12
c0dcad02 13sub defined : method {
56803bef 14 my ($attr, $reader, $writer) = @_;
c0dcad02 15 return sub { CORE::defined $reader->($_[0])->{$_[1]} ? 1 : 0 };
56803bef 16}
c0dcad02 17
9a976497 18sub get : method {
56803bef 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) = @_;
56803bef 32 return sub { CORE::keys %{$reader->($_[0])} };
9a976497 33}
56803bef 34
9a976497 35sub values : method {
36 my ($attr, $reader, $writer) = @_;
56803bef 37 return sub { CORE::values %{$reader->($_[0])} };
38}
9a976497 39
40sub kv : method {
41 my ($attr, $reader, $writer) = @_;
56803bef 42 return sub {
9a976497 43 my $h = $reader->($_[0]);
44 map {
45 [ $_, $h->{$_} ]
56803bef 46 } CORE::keys %{$h}
47 };
9a976497 48}
56803bef 49
23940f50 50sub elements : method {
51 my ($attr, $reader, $writer) = @_;
52 return sub {
53 my $h = $reader->($_[0]);
54 map {
55 $_, $h->{$_}
56 } CORE::keys %{$h}
57 };
58}
59
9a976497 60sub count : method {
61 my ($attr, $reader, $writer) = @_;
56803bef 62 return sub { scalar CORE::keys %{$reader->($_[0])} };
9a976497 63}
64
65sub empty : method {
66 my ($attr, $reader, $writer) = @_;
56803bef 67 return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 };
9a976497 68}
69
701;
71
72__END__
73
74=pod
75
76=head1 NAME
77
78MooseX::AttributeHelpers::MethodProvider::ImmutableHash
56803bef 79
9a976497 80=head1 DESCRIPTION
81
56803bef 82This is a role which provides the method generators for
9a976497 83L<MooseX::AttributeHelpers::Collection::ImmutableHash>.
84
85=head1 METHODS
86
87=over 4
88
89=item B<meta>
90
91=back
92
93=head1 PROVIDED METHODS
94
95=over 4
96
97=item B<count>
98
de9d98c6 99Returns the number of elements in the list.
100
9a976497 101=item B<empty>
102
de9d98c6 103If the list is populated, returns true. Otherwise, returns false.
104
9a976497 105=item B<exists>
106
de9d98c6 107Returns true if the given key is present in the hash
108
f82de33d 109=item B<defined>
110
de9d98c6 111Returns true if the value of a given key is defined
112
9a976497 113=item B<get>
114
de9d98c6 115Returns an element of the hash by its key.
116
9a976497 117=item B<keys>
118
de9d98c6 119Returns the list of keys in the hash.
120
9a976497 121=item B<values>
122
de9d98c6 123Returns the list of values in the hash.
124
9a976497 125=item B<kv>
126
23940f50 127Returns the key, value pairs in the hash as array references
128
129=item B<elements>
130
131Returns the key, value pairs in the hash as a flattened list
de9d98c6 132
9a976497 133=back
134
135=head1 BUGS
136
56803bef 137All complex software has bugs lurking in it, and this module is no
9a976497 138exception. If you find a bug please either email me, or add the bug
139to cpan-RT.
140
141=head1 AUTHOR
142
143Stevan Little E<lt>stevan@iinteractive.comE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
9c5d164e 147Copyright 2007-2009 by Infinity Interactive, Inc.
9a976497 148
149L<http://www.iinteractive.com>
150
151This library is free software; you can redistribute it and/or modify
152it under the same terms as Perl itself.
153
154=cut
155