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_weak_ref { exists $_[0]->{weak_ref} }
40 sub has_init_arg { exists $_[0]->{init_arg} }
41 sub has_type_constraint { exists $_[0]->{type_constraint} }
42 sub has_trigger { exists $_[0]->{trigger} }
43 sub has_builder { exists $_[0]->{builder} }
45 sub generate_accessor {
46 my $attribute = shift;
48 my $name = $attribute->name;
49 my $key = $attribute->init_arg;
50 my $default = $attribute->default;
51 my $trigger = $attribute->trigger;
52 my $type = $attribute->type_constraint;
53 my $constraint = $attribute->find_type_constraint;
58 if ($attribute->_is_metadata eq 'rw') {
59 $accessor .= 'if (@_) {
64 my $display = defined($_) ? $_ : "undef";
65 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
69 $accessor .= '$self->{$key} = $_;';
71 if ($attribute->weak_ref) {
72 $accessor .= 'Scalar::Util::weaken($self->{$key});';
76 $accessor .= '$trigger->($self, $_, $attribute);';
84 if ($attribute->is_lazy) {
85 $accessor .= '$self->{$key} = ';
86 $accessor .= ref($default) eq 'CODE'
89 $accessor .= ' if !exists($self->{$key});';
92 $accessor .= 'return $self->{$key}
95 return eval $accessor;
98 sub generate_predicate {
99 my $attribute = shift;
100 my $key = $attribute->init_arg;
102 my $predicate = 'sub { exists($_[0]->{$key}) }';
104 return eval $predicate;
107 sub generate_clearer {
108 my $attribute = shift;
109 my $key = $attribute->init_arg;
111 my $predicate = 'sub { delete($_[0]->{$key}) }';
113 return eval $predicate;
116 sub generate_handles {
117 my $attribute = shift;
118 my $reader = $attribute->name;
122 for my $local_method (keys %{ $attribute->handles }) {
123 my $remote_method = $attribute->handles->{$local_method};
127 $self->$reader->$remote_method(@_)
130 $method_map{$local_method} = eval $method;
137 my ($self, $class, $name, %args) = @_;
139 confess "You must specify a default for lazy attribute '$name'"
140 if $args{lazy} && !exists($args{default});
142 confess "Trigger is not allowed on read-only attribute '$name'"
143 if $args{trigger} && $args{is} ne 'rw';
145 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
146 if ref($args{default})
147 && ref($args{default}) ne 'CODE';
149 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
151 && ref($args{handles}) eq 'ARRAY';
153 confess "You must pass a HASH or ARRAY to handles"
154 if exists($args{handles})
155 && ref($args{handles}) ne 'HASH';
157 $args{type_constraint} = delete $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
213 return 1 if $constraint->($_);
215 my $name = $self->name;
216 local $_ = "undef" unless defined($_);
217 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
226 Mouse::Attribute - attribute metaclass
230 =head2 new %args -> Mouse::Attribute
232 Instantiates a new Mouse::Attribute. Does nothing else.
234 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
236 Creates a new attribute in OwnerClass. Accessors and helper methods are
237 installed. Some error checking is done.
239 =head2 name -> AttributeName
241 =head2 class -> OwnerClass
243 =head2 default -> Value
245 =head2 predicate -> MethodName
247 =head2 clearer -> MethodName
249 =head2 handles -> { LocalName => RemoteName }
251 =head2 weak_ref -> Bool
253 =head2 init_arg -> Str
255 Informational methods.
257 =head2 generate_accessor -> CODE
259 Creates a new code reference for the attribute's accessor.
261 =head2 generate_predicate -> CODE
263 Creates a new code reference for the attribute's predicate.
265 =head2 generate_clearer -> CODE
267 Creates a new code reference for the attribute's clearer.
269 =head2 generate_handles -> { MethodName => CODE }
271 Creates a new code reference for each of the attribute's handles methods.