More methodification, change some names to be what Moose::Meta::Attribute uses
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2package Mouse::Attribute;
3use strict;
4use warnings;
5
6use Carp 'confess';
7
8sub new {
9 my $class = shift;
10 my %args = @_;
11
7ee01d77 12 $args{init_arg} = $args{name}
13 unless exists $args{init_arg};
c3398f5b 14 $args{is} ||= '';
15
16 bless \%args, $class;
17}
18
186657a9 19sub name { $_[0]->{name} }
20sub class { $_[0]->{class} }
2434d21b 21sub _is_metadata { $_[0]->{is} }
22sub is_required { $_[0]->{required} }
186657a9 23sub default { $_[0]->{default} }
2434d21b 24sub is_lazy { $_[0]->{lazy} }
186657a9 25sub predicate { $_[0]->{predicate} }
26sub clearer { $_[0]->{clearer} }
27sub handles { $_[0]->{handles} }
28sub weak_ref { $_[0]->{weak_ref} }
29sub init_arg { $_[0]->{init_arg} }
30sub type_constraint { $_[0]->{type_constraint} }
de9a434a 31sub trigger { $_[0]->{trigger} }
32sub builder { $_[0]->{builder} }
c3398f5b 33
ccea8101 34sub has_default { exists $_[0]->{default} }
35sub has_predicate { exists $_[0]->{predicate} }
36sub has_clearer { exists $_[0]->{clearer} }
37sub has_handles { exists $_[0]->{handles} }
38sub has_weak_ref { exists $_[0]->{weak_ref} }
39sub has_init_arg { exists $_[0]->{init_arg} }
40sub has_type_constraint { exists $_[0]->{type_constraint} }
de9a434a 41sub has_trigger { exists $_[0]->{trigger} }
42sub has_builder { exists $_[0]->{builder} }
ccea8101 43
c3398f5b 44sub generate_accessor {
45 my $attribute = shift;
46
2434d21b 47 my $name = $attribute->name;
48 my $key = $attribute->init_arg;
49 my $default = $attribute->default;
50 my $trigger = $attribute->trigger;
51 my $type = $attribute->type_constraint;
5aa30ced 52 my $constraint = $attribute->find_type_constraint;
c3398f5b 53
54 my $accessor = 'sub {
55 my $self = shift;';
56
2434d21b 57 if ($attribute->_is_metadata eq 'rw') {
c3398f5b 58 $accessor .= 'if (@_) {
a707f587 59 local $_ = $_[0];';
60
61 if ($constraint) {
1f2b4780 62 $accessor .= 'do {
63 my $display = defined($_) ? $_ : "undef";
64 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
65 };'
a707f587 66 }
67
68 $accessor .= '$self->{$key} = $_;';
c3398f5b 69
2434d21b 70 if ($attribute->weak_ref) {
c3398f5b 71 $accessor .= 'Scalar::Util::weaken($self->{$key});';
72 }
73
74 if ($trigger) {
a707f587 75 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 76 }
77
78 $accessor .= '}';
79 }
80 else {
81 }
82
2434d21b 83 if ($attribute->is_lazy) {
c3398f5b 84 $accessor .= '$self->{$key} = ';
2434d21b 85 $accessor .= ref($default) eq 'CODE'
c3398f5b 86 ? '$default->($self)'
87 : '$default';
88 $accessor .= ' if !exists($self->{$key});';
89 }
90
91 $accessor .= 'return $self->{$key}
92 }';
93
94 return eval $accessor;
95}
96
97sub generate_predicate {
98 my $attribute = shift;
2434d21b 99 my $key = $attribute->init_arg;
c3398f5b 100
101 my $predicate = 'sub { exists($_[0]->{$key}) }';
102
103 return eval $predicate;
104}
105
106sub generate_clearer {
107 my $attribute = shift;
2434d21b 108 my $key = $attribute->init_arg;
c3398f5b 109
110 my $predicate = 'sub { delete($_[0]->{$key}) }';
111
112 return eval $predicate;
113}
114
115sub generate_handles {
116 my $attribute = shift;
2434d21b 117 my $reader = $attribute->name;
c3398f5b 118
119 my %method_map;
120
2434d21b 121 for my $local_method (keys %{ $attribute->handles }) {
122 my $remote_method = $attribute->handles->{$local_method};
c3398f5b 123
124 my $method = 'sub {
125 my $self = shift;
126 $self->$reader->$remote_method(@_)
127 }';
128
129 $method_map{$local_method} = eval $method;
130 }
131
132 return \%method_map;
133}
134
135sub create {
136 my ($self, $class, $name, %args) = @_;
137
138 confess "You must specify a default for lazy attribute '$name'"
139 if $args{lazy} && !exists($args{default});
140
141 confess "Trigger is not allowed on read-only attribute '$name'"
142 if $args{trigger} && $args{is} ne 'rw';
143
144 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
145 if ref($args{default})
146 && ref($args{default}) ne 'CODE';
147
148 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
149 if $args{handles}
150 && ref($args{handles}) eq 'ARRAY';
151
152 confess "You must pass a HASH or ARRAY to handles"
153 if exists($args{handles})
154 && ref($args{handles}) ne 'HASH';
155
186657a9 156 $args{type_constraint} = delete $args{isa};
157
c3398f5b 158 my $attribute = $self->new(%args, name => $name, class => $class);
159 my $meta = $class->meta;
160
b2500191 161 $meta->add_attribute($attribute);
162
c3398f5b 163 # install an accessor
2434d21b 164 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
c3398f5b 165 my $accessor = $attribute->generate_accessor;
166 no strict 'refs';
167 *{ $class . '::' . $name } = $accessor;
168 }
169
c3398f5b 170 for my $method (qw/predicate clearer/) {
2434d21b 171 my $predicate = "has_$method";
172 if ($attribute->$predicate) {
c3398f5b 173 my $generator = "generate_$method";
174 my $coderef = $attribute->$generator;
175 no strict 'refs';
2434d21b 176 *{ $class . '::' . $attribute->$method } = $coderef;
c3398f5b 177 }
178 }
179
2434d21b 180 if ($attribute->has_handles) {
c3398f5b 181 my $method_map = $attribute->generate_handles;
182 for my $method_name (keys %$method_map) {
183 no strict 'refs';
184 *{ $class . '::' . $method_name } = $method_map->{$method_name};
185 }
186 }
187
188 return $attribute;
189}
190
5aa30ced 191sub find_type_constraint {
192 my $self = shift;
193 my $type = $self->type_constraint;
194
195 return unless $type;
196
197 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
198 return $checker if $checker;
199
200 confess "Unable to parse type constraint '$type'";
201}
202
203sub verify_type_constraint {
204 my $self = shift;
205 local $_ = shift;
206
207 my $type = $self->type_constraint
208 or return 1;
209 my $constraint = $self->find_type_constraint
210 or return 1;
211
212 return 1 if $constraint->($_);
213
214 my $name = $self->name;
1f2b4780 215 local $_ = "undef" unless defined($_);
5aa30ced 216 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
217}
218
c3398f5b 2191;
220
221__END__
222
223=head1 NAME
224
225Mouse::Attribute - attribute metaclass
226
227=head1 METHODS
228
229=head2 new %args -> Mouse::Attribute
230
231Instantiates a new Mouse::Attribute. Does nothing else.
232
233=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
234
235Creates a new attribute in OwnerClass. Accessors and helper methods are
236installed. Some error checking is done.
237
238=head2 name -> AttributeName
239
240=head2 class -> OwnerClass
241
242=head2 default -> Value
243
244=head2 predicate -> MethodName
245
246=head2 clearer -> MethodName
247
248=head2 handles -> { LocalName => RemoteName }
249
250=head2 weak_ref -> Bool
251
252=head2 init_arg -> Str
253
254Informational methods.
255
256=head2 generate_accessor -> CODE
257
258Creates a new code reference for the attribute's accessor.
259
260=head2 generate_predicate -> CODE
261
262Creates a new code reference for the attribute's predicate.
263
264=head2 generate_clearer -> CODE
265
266Creates a new code reference for the attribute's clearer.
267
268=head2 generate_handles -> { MethodName => CODE }
269
270Creates a new code reference for each of the attribute's handles methods.
271
272=cut
273