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);';
81 $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;';
84 if ($attribute->is_lazy) {
85 $accessor .= '$self->{$key} = ';
87 $accessor .= $attribute->has_builder
89 : ref($default) eq 'CODE'
93 $accessor .= ' if !exists($self->{$key});';
96 $accessor .= 'return $self->{$key}
99 return eval $accessor;
102 sub generate_predicate {
103 my $attribute = shift;
104 my $key = $attribute->init_arg;
106 my $predicate = 'sub { exists($_[0]->{$key}) }';
108 return eval $predicate;
111 sub generate_clearer {
112 my $attribute = shift;
113 my $key = $attribute->init_arg;
115 my $predicate = 'sub { delete($_[0]->{$key}) }';
117 return eval $predicate;
120 sub generate_handles {
121 my $attribute = shift;
122 my $reader = $attribute->name;
126 for my $local_method (keys %{ $attribute->handles }) {
127 my $remote_method = $attribute->handles->{$local_method};
131 $self->$reader->$remote_method(@_)
134 $method_map{$local_method} = eval $method;
141 my ($self, $class, $name, %args) = @_;
143 confess "You must specify a default for lazy attribute '$name'"
144 if $args{lazy} && !exists($args{default}) && !exists($args{builder});
146 confess "Trigger is not allowed on read-only attribute '$name'"
147 if $args{trigger} && $args{is} ne 'rw';
149 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
150 if ref($args{default})
151 && ref($args{default}) ne 'CODE';
153 $args{handles} = { $self->_canonicalize_handles($args{handles}) }
156 $args{type_constraint} = delete $args{isa}
157 if exists $args{isa};
159 my $attribute = $self->new(%args, name => $name, class => $class);
160 my $meta = $class->meta;
162 $meta->add_attribute($attribute);
164 # install an accessor
165 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
166 my $accessor = $attribute->generate_accessor;
168 *{ $class . '::' . $name } = $accessor;
171 for my $method (qw/predicate clearer/) {
172 my $predicate = "has_$method";
173 if ($attribute->$predicate) {
174 my $generator = "generate_$method";
175 my $coderef = $attribute->$generator;
177 *{ $class . '::' . $attribute->$method } = $coderef;
181 if ($attribute->has_handles) {
182 my $method_map = $attribute->generate_handles;
183 for my $method_name (keys %$method_map) {
185 *{ $class . '::' . $method_name } = $method_map->{$method_name};
192 sub find_type_constraint {
194 my $type = $self->type_constraint;
198 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
199 return $checker if $checker;
201 return sub { blessed($_) && blessed($_) eq $type };
204 sub verify_type_constraint {
208 my $type = $self->type_constraint
210 my $constraint = $self->find_type_constraint;
212 return 1 if $constraint->($_);
214 my $name = $self->name;
215 local $_ = "undef" unless defined($_);
216 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
219 sub _canonicalize_handles {
223 if (ref($handles) eq 'HASH') {
226 elsif (ref($handles) eq 'ARRAY') {
227 return map { $_ => $_ } @$handles;
230 confess "Unable to canonicalize the 'handles' option with $handles";
240 Mouse::Attribute - attribute metaclass
244 =head2 new %args -> Mouse::Attribute
246 Instantiates a new Mouse::Attribute. Does nothing else.
248 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
250 Creates a new attribute in OwnerClass. Accessors and helper methods are
251 installed. Some error checking is done.
253 =head2 name -> AttributeName
255 =head2 class -> OwnerClass
257 =head2 is_required -> Bool
259 =head2 default -> Item
261 =head2 has_default -> Bool
263 =head2 is_lazy -> Bool
265 =head2 predicate -> MethodName | Undef
267 =head2 has_predicate -> Bool
269 =head2 clearer -> MethodName | Undef
271 =head2 has_clearer -> Bool
273 =head2 handles -> { LocalName => RemoteName }
275 =head2 has_handles -> Bool
277 =head2 weak_ref -> Bool
279 =head2 init_arg -> Str
281 =head2 type_constraint -> Str
283 =head2 has_type_constraint -> Bool
285 =head2 trigger => CODE | Undef
287 =head2 has_trigger -> Bool
289 =head2 builder => MethodName | Undef
291 =head2 has_builder -> Bool
293 Informational methods.
295 =head2 generate_accessor -> CODE
297 Creates a new code reference for the attribute's accessor.
299 =head2 generate_predicate -> CODE
301 Creates a new code reference for the attribute's predicate.
303 =head2 generate_clearer -> CODE
305 Creates a new code reference for the attribute's clearer.
307 =head2 generate_handles -> { MethodName => CODE }
309 Creates a new code reference for each of the attribute's handles methods.
311 =head2 find_type_constraint -> CODE
313 Returns a code reference which can be used to check that a given value passes
314 this attribute's type constraint;
316 =head2 verify_type_constraint Item -> 1 | ERROR
318 Checks that the given value passes this attribute's type constraint. Returns 1
319 on success, otherwise C<confess>es.