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