1 package Moose::Meta::Attribute::Native;
4 $VERSION = eval $VERSION;
5 our $AUTHORITY = 'cpan:STEVAN';
7 my @trait_names = qw(Bool Counter Number String Array Hash Code);
9 for my $trait_name (@trait_names) {
10 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
11 my $meta = Class::MOP::Class->initialize(
12 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
14 if ($meta->find_method_by_name('register_implementation')) {
15 my $class = $meta->name->register_implementation;
17 "An implementation for $trait_name already exists " .
18 "(found '$class' when trying to register '$trait_class')"
21 $meta->add_method(register_implementation => sub {
22 # resolve_metatrait_alias will load classes anyway, but throws away
23 # their error message; we WANT to die if there's a problem
24 Class::MOP::load_class($trait_class);
37 Moose::Meta::Attribute::Native - Delegate to native Perl types
47 isa => 'HashRef[Str]',
48 default => sub { {} },
50 exists_in_mapping => 'exists',
51 ids_in_mapping => 'keys',
54 set_quantity => [ set => 'quantity' ],
58 my $obj = MyClass->new;
59 $obj->set_quantity(10); # quantity => 10
60 $obj->set_mapping('foo', 4); # foo => 4
61 $obj->set_mapping('bar', 5); # bar => 5
62 $obj->set_mapping('baz', 6); # baz => 6
65 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
67 # prints 'quantity, foo, bar, baz'
68 print join ', ', $obj->ids_in_mapping;
72 Native delegations allow you to delegate to native Perl data
73 structure as if they were objects. For example, in the L</SYNOPSIS> you can
74 see a hash reference being treated as if it has methods named C<exists()>,
75 C<keys()>, C<get()>, and C<set()>.
77 The delegation methods (mostly) map to Perl builtins and operators. The return
78 values of these delegations should be the same as the corresponding Perl
79 operation. Any deviations will be explicitly documented.
83 Native delegations are enabled by passing certain options to C<has> when
84 creating an attribute.
88 To enable this feature, pass the appropriate name in the C<traits> array
89 reference for the attribute. For example, to enable this feature for hash
90 reference, we include C<'Hash'> in the list of traits.
94 You will need to make sure that the attribute has an appropriate type. For
95 example, to use this with a Hash you must specify that your attribute is some
98 If you I<don't> specify a type, each trait has a default type it will use.
102 This is just like any other delegation, but only a hash reference is allowed
103 when defining native delegations. The keys are the methods to be created in
104 the class which contains the attribute. The values are the methods provided by
105 the associated trait. Currying works the same way as it does with any other
108 See the docs for each native trait for details on what methods are available.
110 =head2 default or builder
112 Some traits provide a "default default" for historical reasons. This behavior
113 is deprecated, and you are strongly encouraged to provide a default value or
114 make the attribute required.
116 =head1 TRAITS FOR NATIVE DELEGATIONS
120 =item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
125 isa => 'ArrayRef[Str]',
126 default => sub { [] },
129 next_item => 'shift',
134 =item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
144 flip_switch => 'toggle',
150 =item L<Code|Moose::Meta::Attribute::Native::Trait::Code>
165 =item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
168 traits => ['Counter'],
173 inc_counter => 'inc',
174 dec_counter => 'dec',
175 reset_counter => 'reset',
180 =item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
185 isa => 'HashRef[Str]',
186 default => sub { {} },
190 has_option => 'exists',
195 =item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
198 traits => ['Number'],
214 =item L<String|Moose::Meta::Attribute::Native::Trait::String>
217 traits => ['String'],
222 add_text => 'append',
223 replace_text => 'replace',
230 =head1 COMPATIBILITY WITH MooseX::AttributeHelpers
232 This feature used to be a separated CPAN distribution called
233 L<MooseX::AttributeHelpers>.
235 When the feature was incorporated into the Moose core, some of the API details
236 were changed. The underlying capabilities are the same, but some details of
237 the API were changed.
241 See L<Moose/BUGS> for details on reporting bugs.
245 Stevan Little E<lt>stevan@iinteractive.comE<gt>
247 B<with contributions from:>
251 Paul (frodwith) Driver
255 Chris (perigrin) Prather
257 Robert (phaylon) Sedlacek
267 Florian (rafl) Ragwitz
277 =head1 COPYRIGHT AND LICENSE
279 Copyright 2007-2009 by Infinity Interactive, Inc.
281 L<http://www.iinteractive.com>
283 This library is free software; you can redistribute it and/or modify
284 it under the same terms as Perl itself.