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