Use methods on attribute instead of poking directly in the hash
[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} }
de9a434a 21sub required { $_[0]->{required} }
186657a9 22sub default { $_[0]->{default} }
de9a434a 23sub lazy { $_[0]->{lazy} }
186657a9 24sub predicate { $_[0]->{predicate} }
25sub clearer { $_[0]->{clearer} }
26sub handles { $_[0]->{handles} }
27sub weak_ref { $_[0]->{weak_ref} }
28sub init_arg { $_[0]->{init_arg} }
29sub type_constraint { $_[0]->{type_constraint} }
de9a434a 30sub trigger { $_[0]->{trigger} }
31sub builder { $_[0]->{builder} }
c3398f5b 32
ccea8101 33sub has_default { exists $_[0]->{default} }
34sub has_predicate { exists $_[0]->{predicate} }
35sub has_clearer { exists $_[0]->{clearer} }
36sub has_handles { exists $_[0]->{handles} }
37sub has_weak_ref { exists $_[0]->{weak_ref} }
38sub has_init_arg { exists $_[0]->{init_arg} }
39sub has_type_constraint { exists $_[0]->{type_constraint} }
de9a434a 40sub has_trigger { exists $_[0]->{trigger} }
41sub has_builder { exists $_[0]->{builder} }
ccea8101 42
c3398f5b 43sub generate_accessor {
44 my $attribute = shift;
45
a707f587 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};
5aa30ced 51 my $constraint = $attribute->find_type_constraint;
c3398f5b 52
53 my $accessor = 'sub {
54 my $self = shift;';
55
56 if ($attribute->{is} eq 'rw') {
57 $accessor .= 'if (@_) {
a707f587 58 local $_ = $_[0];';
59
60 if ($constraint) {
1f2b4780 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 };'
a707f587 65 }
66
67 $accessor .= '$self->{$key} = $_;';
c3398f5b 68
69 if ($attribute->{weak_ref}) {
70 $accessor .= 'Scalar::Util::weaken($self->{$key});';
71 }
72
73 if ($trigger) {
a707f587 74 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 75 }
76
77 $accessor .= '}';
78 }
79 else {
80 }
81
82 if ($attribute->{lazy}) {
83 $accessor .= '$self->{$key} = ';
84 $accessor .= ref($attribute->{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
96sub 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
105sub 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
114sub 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
134sub 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
186657a9 155 $args{type_constraint} = delete $args{isa};
156
c3398f5b 157 my $attribute = $self->new(%args, name => $name, class => $class);
158 my $meta = $class->meta;
159
b2500191 160 $meta->add_attribute($attribute);
161
c3398f5b 162 # install an accessor
163 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
164 my $accessor = $attribute->generate_accessor;
165 no strict 'refs';
166 *{ $class . '::' . $name } = $accessor;
167 }
168
c3398f5b 169 for my $method (qw/predicate clearer/) {
170 if (exists $attribute->{$method}) {
171 my $generator = "generate_$method";
172 my $coderef = $attribute->$generator;
173 no strict 'refs';
174 *{ $class . '::' . $attribute->{$method} } = $coderef;
175 }
176 }
177
178 if ($attribute->{handles}) {
179 my $method_map = $attribute->generate_handles;
180 for my $method_name (keys %$method_map) {
181 no strict 'refs';
182 *{ $class . '::' . $method_name } = $method_map->{$method_name};
183 }
184 }
185
186 return $attribute;
187}
188
5aa30ced 189sub find_type_constraint {
190 my $self = shift;
191 my $type = $self->type_constraint;
192
193 return unless $type;
194
195 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
196 return $checker if $checker;
197
198 confess "Unable to parse type constraint '$type'";
199}
200
201sub verify_type_constraint {
202 my $self = shift;
203 local $_ = shift;
204
205 my $type = $self->type_constraint
206 or return 1;
207 my $constraint = $self->find_type_constraint
208 or return 1;
209
210 return 1 if $constraint->($_);
211
212 my $name = $self->name;
1f2b4780 213 local $_ = "undef" unless defined($_);
5aa30ced 214 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
215}
216
c3398f5b 2171;
218
219__END__
220
221=head1 NAME
222
223Mouse::Attribute - attribute metaclass
224
225=head1 METHODS
226
227=head2 new %args -> Mouse::Attribute
228
229Instantiates a new Mouse::Attribute. Does nothing else.
230
231=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
232
233Creates a new attribute in OwnerClass. Accessors and helper methods are
234installed. Some error checking is done.
235
236=head2 name -> AttributeName
237
238=head2 class -> OwnerClass
239
240=head2 default -> Value
241
242=head2 predicate -> MethodName
243
244=head2 clearer -> MethodName
245
246=head2 handles -> { LocalName => RemoteName }
247
248=head2 weak_ref -> Bool
249
250=head2 init_arg -> Str
251
252Informational methods.
253
254=head2 generate_accessor -> CODE
255
256Creates a new code reference for the attribute's accessor.
257
258=head2 generate_predicate -> CODE
259
260Creates a new code reference for the attribute's predicate.
261
262=head2 generate_clearer -> CODE
263
264Creates a new code reference for the attribute's clearer.
265
266=head2 generate_handles -> { MethodName => CODE }
267
268Creates a new code reference for each of the attribute's handles methods.
269
270=cut
271