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