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