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;
56 if ($attribute->_is_metadata eq 'rw') {
57 $accessor .= 'if (@_) {
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->();
67 $accessor .= '$self->{$key} = $_;';
69 if ($attribute->weak_ref) {
70 $accessor .= 'Scalar::Util::weaken($self->{$key});';
74 $accessor .= '$trigger->($self, $_, $attribute);';
82 if ($attribute->is_lazy) {
83 $accessor .= '$self->{$key} = ';
84 $accessor .= ref($default) eq 'CODE'
87 $accessor .= ' if !exists($self->{$key});';
90 $accessor .= 'return $self->{$key}
93 return eval $accessor;
96 sub generate_predicate {
97 my $attribute = shift;
98 my $key = $attribute->init_arg;
100 my $predicate = 'sub { exists($_[0]->{$key}) }';
102 return eval $predicate;
105 sub generate_clearer {
106 my $attribute = shift;
107 my $key = $attribute->init_arg;
109 my $predicate = 'sub { delete($_[0]->{$key}) }';
111 return eval $predicate;
114 sub generate_handles {
115 my $attribute = shift;
116 my $reader = $attribute->name;
120 for my $local_method (keys %{ $attribute->handles }) {
121 my $remote_method = $attribute->handles->{$local_method};
125 $self->$reader->$remote_method(@_)
128 $method_map{$local_method} = eval $method;
135 my ($self, $class, $name, %args) = @_;
137 confess "You must specify a default for lazy attribute '$name'"
138 if $args{lazy} && !exists($args{default});
140 confess "Trigger is not allowed on read-only attribute '$name'"
141 if $args{trigger} && $args{is} ne 'rw';
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';
147 $args{handles} = { $self->_canonicalize_handles($args{handles}) }
150 $args{type_constraint} = delete $args{isa};
152 my $attribute = $self->new(%args, name => $name, class => $class);
153 my $meta = $class->meta;
155 $meta->add_attribute($attribute);
157 # install an accessor
158 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
159 my $accessor = $attribute->generate_accessor;
161 *{ $class . '::' . $name } = $accessor;
164 for my $method (qw/predicate clearer/) {
165 my $predicate = "has_$method";
166 if ($attribute->$predicate) {
167 my $generator = "generate_$method";
168 my $coderef = $attribute->$generator;
170 *{ $class . '::' . $attribute->$method } = $coderef;
174 if ($attribute->has_handles) {
175 my $method_map = $attribute->generate_handles;
176 for my $method_name (keys %$method_map) {
178 *{ $class . '::' . $method_name } = $method_map->{$method_name};
185 sub find_type_constraint {
187 my $type = $self->type_constraint;
191 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
192 return $checker if $checker;
194 return sub { blessed($_) && blessed($_) eq $type };
197 sub verify_type_constraint {
201 my $type = $self->type_constraint
203 my $constraint = $self->find_type_constraint;
205 return 1 if $constraint->($_);
207 my $name = $self->name;
208 local $_ = "undef" unless defined($_);
209 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
212 sub _canonicalize_handles {
216 if (ref($handles) eq 'HASH') {
219 elsif (ref($handles) eq 'ARRAY') {
220 return map { $_ => $_ } @$handles;
223 confess "Unable to canonicalize the 'handles' option with $handles";
233 Mouse::Attribute - attribute metaclass
237 =head2 new %args -> Mouse::Attribute
239 Instantiates a new Mouse::Attribute. Does nothing else.
241 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
243 Creates a new attribute in OwnerClass. Accessors and helper methods are
244 installed. Some error checking is done.
246 =head2 name -> AttributeName
248 =head2 class -> OwnerClass
250 =head2 is_required -> Bool
252 =head2 default -> Item
254 =head2 has_default -> Bool
256 =head2 is_lazy -> Bool
258 =head2 predicate -> MethodName | Undef
260 =head2 has_predicate -> Bool
262 =head2 clearer -> MethodName | Undef
264 =head2 has_clearer -> Bool
266 =head2 handles -> { LocalName => RemoteName }
268 =head2 has_handles -> Bool
270 =head2 weak_ref -> Bool
272 =head2 init_arg -> Str
274 =head2 type_constraint -> Str
276 =head2 has_type_constraint -> Bool
278 =head2 trigger => CODE | Undef
280 =head2 has_trigger -> Bool
282 =head2 builder => MethodName | Undef
284 =head2 has_builder -> Bool
286 Informational methods.
288 =head2 generate_accessor -> CODE
290 Creates a new code reference for the attribute's accessor.
292 =head2 generate_predicate -> CODE
294 Creates a new code reference for the attribute's predicate.
296 =head2 generate_clearer -> CODE
298 Creates a new code reference for the attribute's clearer.
300 =head2 generate_handles -> { MethodName => CODE }
302 Creates a new code reference for each of the attribute's handles methods.