2 package Moose::Attribute::Native;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
8 my @trait_names = qw(Bool Counter Number String Array Hash);
10 for my $trait_name (@trait_names) {
11 my $trait_class = "Moose::Meta::Attribute::Trait::Native::$trait_name";
12 my $meta = Class::MOP::Class->initialize(
13 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
15 if ($meta->find_method_by_name('register_implementation')) {
16 my $class = $meta->name->register_implementation;
18 "An implementation for $trait_name already exists " .
19 "(found '$class' when trying to register '$trait_class')"
22 $meta->add_method(register_implementation => sub {
23 # resolve_metatrait_alias will load classes anyway, but throws away
24 # their error message; we WANT to die if there's a problem
25 Class::MOP::load_class($trait_class);
38 Moose::Attribute::Native - Extend your attribute interfaces
44 use Moose::Attribute::Native;
49 isa => 'HashRef[Str]',
50 default => sub { {} },
52 exists_in_mapping => 'exists',
53 ids_in_mapping => 'keys',
56 set_quantity => [ set => [ 'quantity' ] ],
63 my $obj = MyClass->new;
64 $obj->set_quantity(10); # quantity => 10
65 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
66 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
67 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
71 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
74 print join ', ', $obj->ids_in_mapping;
78 While L<Moose> attributes provide you with a way to name your accessors,
79 readers, writers, clearers and predicates, this library provides commonly
80 used attribute helper methods for more specific types of data.
82 As seen in the L</SYNOPSIS>, you specify the extension via the
83 C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>.
85 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
86 very commonly used, so we moved it into core Moose. Since this gave us a chance
87 to change the interface, you will have to change your code or continue using
88 the L<MooseX::AttributeHelpers> extension.
94 This is like C<< handles >> in L<Moose/has>, but only HASH references are
95 allowed. Keys are method names that you want installed locally, and values are
96 methods from the method providers (below). Currying with delegated methods
97 works normally for C<< handles >>.
99 =head1 METHOD PROVIDERS
103 =item L<Number|Moose::Meta::Attribute::Trait::Native::Number>
105 Common numerical operations.
107 =item L<String|Moose::Meta::Attribute::Trait::Native::String>
109 Common methods for string operations.
111 =item L<Counter|Moose::Meta::Attribute::Trait::Native::Counter>
113 Methods for incrementing and decrementing a counter attribute.
115 =item L<Bool|Moose::Meta::Attribute::Trait::Native::Bool>
117 Common methods for boolean values.
119 =item L<Hash|Moose::Meta::Attribute::Trait::Native::Hash>
121 Common methods for hash references.
123 =item L<ImmutableHash|Moose::Meta::Attribute::Trait::Native::ImmutableHash>
125 Common methods for inspecting hash references.
127 =item L<Array|Moose::Meta::Attribute::Trait::Native::Array>
129 Common methods for array references.
131 =item L<List|Moose::Meta::Attribute::Trait::Native::List>
133 Common list methods for array references.
139 All complex software has bugs lurking in it, and this module is no
140 exception. If you find a bug please either email me, or add the bug
145 Stevan Little E<lt>stevan@iinteractive.comE<gt>
147 B<with contributions from:>
151 Paul (frodwith) Driver
155 Chris (perigrin) Prather
157 Robert (phaylon) Sedlacek
167 Florian (rafl) Ragwitz
173 =head1 COPYRIGHT AND LICENSE
175 Copyright 2007-2009 by Infinity Interactive, Inc.
177 L<http://www.iinteractive.com>
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself.