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