Commit | Line | Data |
e3c07b19 |
1 | |
fafc8b9b |
2 | package Moose::Attribute::Native; |
e3c07b19 |
3 | |
96539d20 |
4 | our $VERSION = '0.87'; |
e3c07b19 |
5 | $VERSION = eval $VERSION; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
fafc8b9b |
8 | my @trait_names = qw(Bool Counter Number String Array Hash); |
9 | |
10 | for my $trait_name (@trait_names) { |
c466e58f |
11 | my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name"; |
fafc8b9b |
12 | my $meta = Class::MOP::Class->initialize( |
13 | "Moose::Meta::Attribute::Custom::Trait::$trait_name" |
14 | ); |
15 | if ($meta->find_method_by_name('register_implementation')) { |
16 | my $class = $meta->name->register_implementation; |
17 | Moose->throw_error( |
18 | "An implementation for $trait_name already exists " . |
19 | "(found '$class' when trying to register '$trait_class')" |
20 | ); |
21 | } |
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); |
26 | return $trait_class; |
27 | }); |
28 | } |
e3c07b19 |
29 | |
e3c07b19 |
30 | 1; |
31 | |
32 | __END__ |
33 | |
34 | =pod |
35 | |
36 | =head1 NAME |
37 | |
fafc8b9b |
38 | Moose::Attribute::Native - Extend your attribute interfaces |
e3c07b19 |
39 | |
40 | =head1 SYNOPSIS |
41 | |
42 | package MyClass; |
43 | use Moose; |
e3c07b19 |
44 | |
45 | has 'mapping' => ( |
a40b446a |
46 | traits => [ 'Hash' ], |
e3c07b19 |
47 | is => 'rw', |
48 | isa => 'HashRef[Str]', |
49 | default => sub { {} }, |
50 | handles => { |
51 | exists_in_mapping => 'exists', |
52 | ids_in_mapping => 'keys', |
53 | get_mapping => 'get', |
54 | set_mapping => 'set', |
55 | set_quantity => [ set => [ 'quantity' ] ], |
56 | }, |
57 | ); |
58 | |
59 | |
60 | # ... |
61 | |
62 | my $obj = MyClass->new; |
63 | $obj->set_quantity(10); # quantity => 10 |
64 | $obj->set_mapping(4, 'foo'); # 4 => 'foo' |
65 | $obj->set_mapping(5, 'bar'); # 5 => 'bar' |
66 | $obj->set_mapping(6, 'baz'); # 6 => 'baz' |
67 | |
68 | |
69 | # prints 'bar' |
70 | print $obj->get_mapping(5) if $obj->exists_in_mapping(5); |
71 | |
72 | # prints '4, 5, 6' |
73 | print join ', ', $obj->ids_in_mapping; |
74 | |
75 | =head1 DESCRIPTION |
76 | |
77 | While L<Moose> attributes provide you with a way to name your accessors, |
78 | readers, writers, clearers and predicates, this library provides commonly |
79 | used attribute helper methods for more specific types of data. |
80 | |
81 | As seen in the L</SYNOPSIS>, you specify the extension via the |
5f3663b2 |
82 | C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>. |
e3c07b19 |
83 | |
87b4e821 |
84 | This module used to exist as the L<MooseX::AttributeHelpers> extension. It was |
85 | very commonly used, so we moved it into core Moose. Since this gave us a chance |
86 | to change the interface, you will have to change your code or continue using |
87 | the L<MooseX::AttributeHelpers> extension. |
88 | |
e3c07b19 |
89 | =head1 PARAMETERS |
90 | |
91 | =head2 handles |
92 | |
5f3663b2 |
93 | This is like C<< handles >> in L<Moose/has>, but only HASH references are |
94 | allowed. Keys are method names that you want installed locally, and values are |
87b4e821 |
95 | methods from the method providers (below). Currying with delegated methods |
96 | works normally for C<< handles >>. |
e3c07b19 |
97 | |
98 | =head1 METHOD PROVIDERS |
99 | |
100 | =over |
101 | |
c466e58f |
102 | =item L<Number|Moose::Meta::Attribute::Native::Trait::Number> |
e3c07b19 |
103 | |
104 | Common numerical operations. |
105 | |
c466e58f |
106 | =item L<String|Moose::Meta::Attribute::Native::Trait::String> |
e3c07b19 |
107 | |
108 | Common methods for string operations. |
109 | |
c466e58f |
110 | =item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter> |
e3c07b19 |
111 | |
112 | Methods for incrementing and decrementing a counter attribute. |
113 | |
c466e58f |
114 | =item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool> |
e3c07b19 |
115 | |
116 | Common methods for boolean values. |
117 | |
c466e58f |
118 | =item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash> |
e3c07b19 |
119 | |
120 | Common methods for hash references. |
121 | |
c466e58f |
122 | =item L<ImmutableHash|Moose::Meta::Attribute::Native::Trait::ImmutableHash> |
e3c07b19 |
123 | |
124 | Common methods for inspecting hash references. |
125 | |
c466e58f |
126 | =item L<Array|Moose::Meta::Attribute::Native::Trait::Array> |
e3c07b19 |
127 | |
128 | Common methods for array references. |
129 | |
c466e58f |
130 | =item L<List|Moose::Meta::Attribute::Native::Trait::List> |
e3c07b19 |
131 | |
132 | Common list methods for array references. |
133 | |
134 | =back |
135 | |
e3c07b19 |
136 | =head1 BUGS |
137 | |
138 | All complex software has bugs lurking in it, and this module is no |
139 | exception. If you find a bug please either email me, or add the bug |
140 | to cpan-RT. |
141 | |
142 | =head1 AUTHOR |
143 | |
144 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
145 | |
146 | B<with contributions from:> |
147 | |
148 | Robert (rlb3) Boone |
149 | |
150 | Paul (frodwith) Driver |
151 | |
152 | Shawn (Sartak) Moore |
153 | |
154 | Chris (perigrin) Prather |
155 | |
156 | Robert (phaylon) Sedlacek |
157 | |
158 | Tom (dec) Lanyon |
159 | |
160 | Yuval Kogman |
161 | |
162 | Jason May |
163 | |
164 | Cory (gphat) Watson |
165 | |
166 | Florian (rafl) Ragwitz |
167 | |
168 | Evan Carroll |
169 | |
170 | Jesse (doy) Luehrs |
171 | |
172 | =head1 COPYRIGHT AND LICENSE |
173 | |
174 | Copyright 2007-2009 by Infinity Interactive, Inc. |
175 | |
176 | L<http://www.iinteractive.com> |
177 | |
178 | This library is free software; you can redistribute it and/or modify |
179 | it under the same terms as Perl itself. |
180 | |
181 | =cut |