Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Hash; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
bb8ef151 |
5 | our $VERSION = '1.9900'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
c466e58f |
9 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
10 | |
2e069f5a |
11 | sub _helper_type { 'HashRef' } |
e3c07b19 |
12 | |
13 | no Moose::Role; |
14 | |
e3c07b19 |
15 | 1; |
16 | |
17 | __END__ |
18 | |
19 | =pod |
20 | |
21 | =head1 NAME |
22 | |
2420461c |
23 | Moose::Meta::Attribute::Native::Trait::Hash - Helper trait for HashRef attributes |
e3c07b19 |
24 | |
25 | =head1 SYNOPSIS |
26 | |
27 | package Stuff; |
28 | use Moose; |
e3c07b19 |
29 | |
30 | has 'options' => ( |
96fd0bec |
31 | traits => ['Hash'], |
e3c07b19 |
32 | is => 'ro', |
33 | isa => 'HashRef[Str]', |
34 | default => sub { {} }, |
5f3663b2 |
35 | handles => { |
af44c00c |
36 | set_option => 'set', |
37 | get_option => 'get', |
bb023e15 |
38 | has_no_options => 'is_empty', |
af44c00c |
39 | num_options => 'count', |
40 | delete_option => 'delete', |
43852ddb |
41 | option_pairs => 'kv', |
9610c1d2 |
42 | }, |
e3c07b19 |
43 | ); |
44 | |
45 | =head1 DESCRIPTION |
46 | |
7795e4de |
47 | This trait provides native delegation methods for array references. |
96fd0bec |
48 | |
49 | =head1 PROVIDED METHODS |
50 | |
96fd0bec |
51 | =over 4 |
52 | |
85592815 |
53 | =item B<get($key, $key2, $key3...)> |
96fd0bec |
54 | |
85592815 |
55 | Returns values from the hash. |
96fd0bec |
56 | |
e132fd56 |
57 | In list context it returns a list of values in the hash for the given keys. In |
58 | scalar context it returns the value for the last key specified. |
59 | |
60 | This method requires at least one argument. |
96fd0bec |
61 | |
9d010f57 |
62 | =item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)> |
85592815 |
63 | |
e132fd56 |
64 | Sets the elements in the hash to the given values. It returns the new values |
65 | set for each key, in the same order as the keys passed to the method. |
66 | |
67 | This method requires at least two arguments, and expects an even number of |
68 | arguments. |
96fd0bec |
69 | |
9d010f57 |
70 | =item B<delete($key, $key2, $key3...)> |
96fd0bec |
71 | |
9d010f57 |
72 | Removes the elements with the given keys. |
96fd0bec |
73 | |
e132fd56 |
74 | In list context it returns a list of values in the hash for the deleted |
75 | keys. In scalar context it returns the value for the last key specified. |
76 | |
8396a437 |
77 | =item B<keys> |
96fd0bec |
78 | |
8396a437 |
79 | Returns the list of keys in the hash. |
96fd0bec |
80 | |
e132fd56 |
81 | This method does not accept any arguments. |
82 | |
8396a437 |
83 | =item B<exists($key)> |
96fd0bec |
84 | |
8396a437 |
85 | Returns true if the given key is present in the hash. |
96fd0bec |
86 | |
e132fd56 |
87 | This method requires a single argument. |
88 | |
8396a437 |
89 | =item B<defined($key)> |
96fd0bec |
90 | |
8396a437 |
91 | Returns true if the value of a given key is defined. |
96fd0bec |
92 | |
e132fd56 |
93 | This method requires a single argument. |
94 | |
96fd0bec |
95 | =item B<values> |
96 | |
97 | Returns the list of values in the hash. |
98 | |
e132fd56 |
99 | This method does not accept any arguments. |
100 | |
96fd0bec |
101 | =item B<kv> |
102 | |
8396a437 |
103 | Returns the key/value pairs in the hash as an array of array references. |
104 | |
105 | for my $pair ( $object->options->pairs ) { |
106 | print "$pair->[0] = $pair->[1]\n"; |
107 | } |
96fd0bec |
108 | |
e132fd56 |
109 | This method does not accept any arguments. |
110 | |
96fd0bec |
111 | =item B<elements> |
112 | |
8396a437 |
113 | Returns the key/value pairs in the hash as a flattened list.. |
96fd0bec |
114 | |
e132fd56 |
115 | This method does not accept any arguments. |
116 | |
8396a437 |
117 | =item B<clear> |
96fd0bec |
118 | |
8396a437 |
119 | Resets the hash to an empty value, like C<%hash = ()>. |
96fd0bec |
120 | |
e132fd56 |
121 | This method does not accept any arguments. |
122 | |
8396a437 |
123 | =item B<count> |
96fd0bec |
124 | |
39e17fef |
125 | Returns the number of elements in the hash. Also useful for not empty: |
126 | C<< has_options => 'count' >>. |
96fd0bec |
127 | |
e132fd56 |
128 | This method does not accept any arguments. |
129 | |
bb023e15 |
130 | =item B<is_empty> |
96fd0bec |
131 | |
8396a437 |
132 | If the hash is populated, returns false. Otherwise, returns true. |
96fd0bec |
133 | |
e132fd56 |
134 | This method does not accept any arguments. |
135 | |
136 | =item B<accessor($key)> |
137 | |
138 | =item B<accessor($key, $value)> |
96fd0bec |
139 | |
8396a437 |
140 | If passed one argument, returns the value of the specified key. If passed two |
141 | arguments, sets the value of the specified key. |
96fd0bec |
142 | |
e132fd56 |
143 | When called as a setter, this method returns the value that was set. |
144 | |
96fd0bec |
145 | =back |
e3c07b19 |
146 | |
fda9aa50 |
147 | Note that C<each> is deliberately omitted, due to its stateful interaction |
148 | with the hash iterator. C<keys> or C<kv> are much safer. |
149 | |
e3c07b19 |
150 | =head1 METHODS |
151 | |
152 | =over 4 |
153 | |
154 | =item B<meta> |
155 | |
e3c07b19 |
156 | =back |
157 | |
158 | =head1 BUGS |
159 | |
d4048ef3 |
160 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
161 | |
162 | =head1 AUTHOR |
163 | |
164 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
165 | |
166 | =head1 COPYRIGHT AND LICENSE |
167 | |
8e5dd3fb |
168 | Copyright 2007-2010 by Infinity Interactive, Inc. |
e3c07b19 |
169 | |
170 | L<http://www.iinteractive.com> |
171 | |
172 | This library is free software; you can redistribute it and/or modify |
173 | it under the same terms as Perl itself. |
174 | |
175 | =cut |