Commit | Line | Data |
9a976497 |
1 | package MooseX::AttributeHelpers::MethodProvider::ImmutableHash; |
2 | use Moose::Role; |
3 | |
9e2db1c2 |
4 | our $VERSION = '0.17'; |
38430345 |
5 | $VERSION = eval $VERSION; |
9a976497 |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | sub exists : method { |
56803bef |
9 | my ($attr, $reader, $writer) = @_; |
9a976497 |
10 | return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 }; |
56803bef |
11 | } |
9a976497 |
12 | |
c0dcad02 |
13 | sub defined : method { |
56803bef |
14 | my ($attr, $reader, $writer) = @_; |
c0dcad02 |
15 | return sub { CORE::defined $reader->($_[0])->{$_[1]} ? 1 : 0 }; |
56803bef |
16 | } |
c0dcad02 |
17 | |
9a976497 |
18 | sub 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 | |
30 | sub keys : method { |
31 | my ($attr, $reader, $writer) = @_; |
56803bef |
32 | return sub { CORE::keys %{$reader->($_[0])} }; |
9a976497 |
33 | } |
56803bef |
34 | |
9a976497 |
35 | sub values : method { |
36 | my ($attr, $reader, $writer) = @_; |
56803bef |
37 | return sub { CORE::values %{$reader->($_[0])} }; |
38 | } |
9a976497 |
39 | |
40 | sub 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 | |
9a976497 |
50 | sub count : method { |
51 | my ($attr, $reader, $writer) = @_; |
56803bef |
52 | return sub { scalar CORE::keys %{$reader->($_[0])} }; |
9a976497 |
53 | } |
54 | |
55 | sub empty : method { |
56 | my ($attr, $reader, $writer) = @_; |
56803bef |
57 | return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 }; |
9a976497 |
58 | } |
59 | |
60 | 1; |
61 | |
62 | __END__ |
63 | |
64 | =pod |
65 | |
66 | =head1 NAME |
67 | |
68 | MooseX::AttributeHelpers::MethodProvider::ImmutableHash |
56803bef |
69 | |
9a976497 |
70 | =head1 DESCRIPTION |
71 | |
56803bef |
72 | This is a role which provides the method generators for |
9a976497 |
73 | L<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 | |
de9d98c6 |
89 | Returns the number of elements in the list. |
90 | |
9a976497 |
91 | =item B<empty> |
92 | |
de9d98c6 |
93 | If the list is populated, returns true. Otherwise, returns false. |
94 | |
9a976497 |
95 | =item B<exists> |
96 | |
de9d98c6 |
97 | Returns true if the given key is present in the hash |
98 | |
f82de33d |
99 | =item B<defined> |
100 | |
de9d98c6 |
101 | Returns true if the value of a given key is defined |
102 | |
9a976497 |
103 | =item B<get> |
104 | |
de9d98c6 |
105 | Returns an element of the hash by its key. |
106 | |
9a976497 |
107 | =item B<keys> |
108 | |
de9d98c6 |
109 | Returns the list of keys in the hash. |
110 | |
9a976497 |
111 | =item B<values> |
112 | |
de9d98c6 |
113 | Returns the list of values in the hash. |
114 | |
9a976497 |
115 | =item B<kv> |
116 | |
de9d98c6 |
117 | Returns the key, value pairs in the hash |
118 | |
9a976497 |
119 | =back |
120 | |
121 | =head1 BUGS |
122 | |
56803bef |
123 | All complex software has bugs lurking in it, and this module is no |
9a976497 |
124 | exception. If you find a bug please either email me, or add the bug |
125 | to cpan-RT. |
126 | |
127 | =head1 AUTHOR |
128 | |
129 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
130 | |
131 | =head1 COPYRIGHT AND LICENSE |
132 | |
99c62fb8 |
133 | Copyright 2007-2008 by Infinity Interactive, Inc. |
9a976497 |
134 | |
135 | L<http://www.iinteractive.com> |
136 | |
137 | This library is free software; you can redistribute it and/or modify |
138 | it under the same terms as Perl itself. |
139 | |
140 | =cut |
141 | |