fix up some docs
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native.pm
CommitLineData
17e5e226 1package Moose::Meta::Attribute::Native;
e3c07b19 2
cdf3cae6 3my @trait_names = qw(Bool Counter Number String Array Hash Code);
fafc8b9b 4
5for my $trait_name (@trait_names) {
c466e58f 6 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
fafc8b9b 7 my $meta = Class::MOP::Class->initialize(
8 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
9 );
10 if ($meta->find_method_by_name('register_implementation')) {
11 my $class = $meta->name->register_implementation;
12 Moose->throw_error(
13 "An implementation for $trait_name already exists " .
14 "(found '$class' when trying to register '$trait_class')"
15 );
16 }
17 $meta->add_method(register_implementation => sub {
18 # resolve_metatrait_alias will load classes anyway, but throws away
19 # their error message; we WANT to die if there's a problem
20 Class::MOP::load_class($trait_class);
21 return $trait_class;
22 });
23}
e3c07b19 24
e3c07b19 251;
26
ad46f524 27# ABSTRACT: Delegate to native Perl types
28
e3c07b19 29__END__
30
31=pod
32
e3c07b19 33=head1 SYNOPSIS
34
35 package MyClass;
36 use Moose;
e3c07b19 37
38 has 'mapping' => (
e132fd56 39 traits => ['Hash'],
40 is => 'rw',
41 isa => 'HashRef[Str]',
42 default => sub { {} },
43 handles => {
e3c07b19 44 exists_in_mapping => 'exists',
45 ids_in_mapping => 'keys',
46 get_mapping => 'get',
47 set_mapping => 'set',
cb562ad2 48 set_quantity => [ set => 'quantity' ],
e3c07b19 49 },
50 );
51
e3c07b19 52 my $obj = MyClass->new;
53 $obj->set_quantity(10); # quantity => 10
2420461c 54 $obj->set_mapping('foo', 4); # foo => 4
55 $obj->set_mapping('bar', 5); # bar => 5
56 $obj->set_mapping('baz', 6); # baz => 6
e3c07b19 57
2420461c 58 # prints 5
59 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
e3c07b19 60
2420461c 61 # prints 'quantity, foo, bar, baz'
e3c07b19 62 print join ', ', $obj->ids_in_mapping;
63
64=head1 DESCRIPTION
65
e132fd56 66Native delegations allow you to delegate to native Perl data
e11cc42d 67structures as if they were objects. For example, in the L</SYNOPSIS> you can
e132fd56 68see a hash reference being treated as if it has methods named C<exists()>,
69C<keys()>, C<get()>, and C<set()>.
70
71The delegation methods (mostly) map to Perl builtins and operators. The return
72values of these delegations should be the same as the corresponding Perl
73operation. Any deviations will be explicitly documented.
74
75=head1 API
e3c07b19 76
e132fd56 77Native delegations are enabled by passing certain options to C<has> when
78creating an attribute.
e3c07b19 79
e132fd56 80=head2 traits
87b4e821 81
e132fd56 82To enable this feature, pass the appropriate name in the C<traits> array
83reference for the attribute. For example, to enable this feature for hash
84reference, we include C<'Hash'> in the list of traits.
85
86=head2 isa
87
88You will need to make sure that the attribute has an appropriate type. For
89example, to use this with a Hash you must specify that your attribute is some
90sort of C<HashRef>.
91
e3c07b19 92=head2 handles
93
e132fd56 94This is just like any other delegation, but only a hash reference is allowed
95when defining native delegations. The keys are the methods to be created in
96the class which contains the attribute. The values are the methods provided by
97the associated trait. Currying works the same way as it does with any other
98delegation.
e3c07b19 99
e132fd56 100See the docs for each native trait for details on what methods are available.
101
b79a1a50 102=head2 is
103
104Some traits provide a default C<is> for historical reasons. This behavior is
c33c3630 105deprecated, and you are strongly encouraged to provide a value. If you don't
ea867421 106plan to read and write the attribute value directly, not passing the C<is>
107option will prevent standard accessor generation.
b79a1a50 108
b6bf6592 109=head2 default or builder
110
b79a1a50 111Some traits provide a default C<default> for historical reasons. This behavior
b6bf6592 112is deprecated, and you are strongly encouraged to provide a default value or
113make the attribute required.
114
e132fd56 115=head1 TRAITS FOR NATIVE DELEGATIONS
e3c07b19 116
117=over
118
66cce11c 119=item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
e3c07b19 120
66cce11c 121 has 'queue' => (
e132fd56 122 traits => ['Array'],
123 is => 'ro',
124 isa => 'ArrayRef[Str]',
125 default => sub { [] },
126 handles => {
66cce11c 127 add_item => 'push',
128 next_item => 'shift',
39ab25ce 129 # ...
52e0d71f 130 }
131 );
132
66cce11c 133=item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
e3c07b19 134
66cce11c 135 has 'is_lit' => (
e132fd56 136 traits => ['Bool'],
137 is => 'ro',
138 isa => 'Bool',
139 default => 0,
140 handles => {
66cce11c 141 illuminate => 'set',
142 darken => 'unset',
143 flip_switch => 'toggle',
144 is_dark => 'not',
145 # ...
146 }
147 );
148
149=item L<Code|Moose::Meta::Attribute::Native::Trait::Code>
150
66cce11c 151 has 'callback' => (
e132fd56 152 traits => ['Code'],
153 is => 'ro',
154 isa => 'CodeRef',
155 default => sub {
156 sub {'called'}
157 },
158 handles => {
66cce11c 159 call => 'execute',
39ab25ce 160 # ...
52e0d71f 161 }
162 );
163
c466e58f 164=item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
e3c07b19 165
52e0d71f 166 has 'counter' => (
e132fd56 167 traits => ['Counter'],
168 is => 'ro',
169 isa => 'Num',
170 default => 0,
171 handles => {
52e0d71f 172 inc_counter => 'inc',
173 dec_counter => 'dec',
174 reset_counter => 'reset',
39ab25ce 175 # ...
52e0d71f 176 }
177 );
178
c466e58f 179=item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
e3c07b19 180
52e0d71f 181 has 'options' => (
e132fd56 182 traits => ['Hash'],
183 is => 'ro',
184 isa => 'HashRef[Str]',
185 default => sub { {} },
186 handles => {
9958cbe1 187 set_option => 'set',
188 get_option => 'get',
189 has_option => 'exists',
39ab25ce 190 # ...
52e0d71f 191 }
192 );
e3c07b19 193
66cce11c 194=item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
e3c07b19 195
66cce11c 196 has 'integer' => (
e132fd56 197 traits => ['Number'],
198 is => 'ro',
199 isa => 'Int',
200 default => 5,
201 handles => {
66cce11c 202 set => 'set',
203 add => 'add',
204 sub => 'sub',
205 mul => 'mul',
206 div => 'div',
207 mod => 'mod',
208 abs => 'abs',
754a4833 209 # ...
210 }
52e0d71f 211 );
e3c07b19 212
66cce11c 213=item L<String|Moose::Meta::Attribute::Native::Trait::String>
b86a4688 214
66cce11c 215 has 'text' => (
e132fd56 216 traits => ['String'],
217 is => 'ro',
218 isa => 'Str',
219 default => q{},
220 handles => {
66cce11c 221 add_text => 'append',
222 replace_text => 'replace',
754a4833 223 # ...
224 }
b86a4688 225 );
226
e3c07b19 227=back
228
e132fd56 229=head1 COMPATIBILITY WITH MooseX::AttributeHelpers
230
231This feature used to be a separated CPAN distribution called
232L<MooseX::AttributeHelpers>.
233
234When the feature was incorporated into the Moose core, some of the API details
235were changed. The underlying capabilities are the same, but some details of
236the API were changed.
237
e3c07b19 238=head1 BUGS
239
d4048ef3 240See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 241
e3c07b19 242=cut