2 package Mouse::Attribute;
7 use Scalar::Util 'blessed';
13 $args{init_arg} = $args{name}
14 unless exists $args{init_arg};
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} }
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} }
43 sub generate_accessor {
44 my $attribute = shift;
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 my $builder = $attribute->builder;
57 if ($attribute->_is_metadata eq 'rw') {
58 $accessor .= 'if (@_) {
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->();
68 $accessor .= '$self->{$key} = $_;';
70 if ($attribute->weak_ref) {
71 $accessor .= 'Scalar::Util::weaken($self->{$key});';
75 $accessor .= '$trigger->($self, $_, $attribute);';
83 if ($attribute->is_lazy) {
84 $accessor .= '$self->{$key} = ';
86 $accessor .= $attribute->has_builder
88 : ref($default) eq 'CODE'
92 $accessor .= ' if !exists($self->{$key});';
95 $accessor .= 'return $self->{$key}
98 return eval $accessor;
101 sub generate_predicate {
102 my $attribute = shift;
103 my $key = $attribute->init_arg;
105 my $predicate = 'sub { exists($_[0]->{$key}) }';
107 return eval $predicate;
110 sub generate_clearer {
111 my $attribute = shift;
112 my $key = $attribute->init_arg;
114 my $predicate = 'sub { delete($_[0]->{$key}) }';
116 return eval $predicate;
119 sub generate_handles {
120 my $attribute = shift;
121 my $reader = $attribute->name;
125 for my $local_method (keys %{ $attribute->handles }) {
126 my $remote_method = $attribute->handles->{$local_method};
130 $self->$reader->$remote_method(@_)
133 $method_map{$local_method} = eval $method;
140 my ($self, $class, $name, %args) = @_;
142 confess "You must specify a default for lazy attribute '$name'"
143 if $args{lazy} && !exists($args{default}) && !exists($args{builder});
145 confess "Trigger is not allowed on read-only attribute '$name'"
146 if $args{trigger} && $args{is} ne 'rw';
148 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
149 if ref($args{default})
150 && ref($args{default}) ne 'CODE';
152 $args{handles} = { $self->_canonicalize_handles($args{handles}) }
155 $args{type_constraint} = delete $args{isa}
156 if exists $args{isa};
158 my $attribute = $self->new(%args, name => $name, class => $class);
159 my $meta = $class->meta;
161 $meta->add_attribute($attribute);
163 # install an accessor
164 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
165 my $accessor = $attribute->generate_accessor;
167 *{ $class . '::' . $name } = $accessor;
170 for my $method (qw/predicate clearer/) {
171 my $predicate = "has_$method";
172 if ($attribute->$predicate) {
173 my $generator = "generate_$method";
174 my $coderef = $attribute->$generator;
176 *{ $class . '::' . $attribute->$method } = $coderef;
180 if ($attribute->has_handles) {
181 my $method_map = $attribute->generate_handles;
182 for my $method_name (keys %$method_map) {
184 *{ $class . '::' . $method_name } = $method_map->{$method_name};
191 sub find_type_constraint {
193 my $type = $self->type_constraint;
197 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
198 return $checker if $checker;
200 return sub { blessed($_) && blessed($_) eq $type };
203 sub verify_type_constraint {
207 my $type = $self->type_constraint
209 my $constraint = $self->find_type_constraint;
211 return 1 if $constraint->($_);
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 $_");
218 sub _canonicalize_handles {
222 if (ref($handles) eq 'HASH') {
225 elsif (ref($handles) eq 'ARRAY') {
226 return map { $_ => $_ } @$handles;
229 confess "Unable to canonicalize the 'handles' option with $handles";
239 Mouse::Attribute - attribute metaclass
243 =head2 new %args -> Mouse::Attribute
245 Instantiates a new Mouse::Attribute. Does nothing else.
247 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
249 Creates a new attribute in OwnerClass. Accessors and helper methods are
250 installed. Some error checking is done.
252 =head2 name -> AttributeName
254 =head2 class -> OwnerClass
256 =head2 is_required -> Bool
258 =head2 default -> Item
260 =head2 has_default -> Bool
262 =head2 is_lazy -> Bool
264 =head2 predicate -> MethodName | Undef
266 =head2 has_predicate -> Bool
268 =head2 clearer -> MethodName | Undef
270 =head2 has_clearer -> Bool
272 =head2 handles -> { LocalName => RemoteName }
274 =head2 has_handles -> Bool
276 =head2 weak_ref -> Bool
278 =head2 init_arg -> Str
280 =head2 type_constraint -> Str
282 =head2 has_type_constraint -> Bool
284 =head2 trigger => CODE | Undef
286 =head2 has_trigger -> Bool
288 =head2 builder => MethodName | Undef
290 =head2 has_builder -> Bool
292 Informational methods.
294 =head2 generate_accessor -> CODE
296 Creates a new code reference for the attribute's accessor.
298 =head2 generate_predicate -> CODE
300 Creates a new code reference for the attribute's predicate.
302 =head2 generate_clearer -> CODE
304 Creates a new code reference for the attribute's clearer.
306 =head2 generate_handles -> { MethodName => CODE }
308 Creates a new code reference for each of the attribute's handles methods.
310 =head2 find_type_constraint -> CODE
312 Returns a code reference which can be used to check that a given value passes
313 this attribute's type constraint;
315 =head2 verify_type_constraint Item -> 1 | ERROR
317 Checks that the given value passes this attribute's type constraint. Returns 1
318 on success, otherwise C<confess>es.