2 package Mouse::Attribute;
12 $args{init_arg} = $args{name}
13 unless exists $args{init_arg};
19 sub name { $_[0]->{name} }
20 sub class { $_[0]->{class} }
21 sub _is_metadata { $_[0]->{is} }
22 sub is_required { $_[0]->{required} }
23 sub default { $_[0]->{default} }
24 sub is_lazy { $_[0]->{lazy} }
25 sub predicate { $_[0]->{predicate} }
26 sub clearer { $_[0]->{clearer} }
27 sub handles { $_[0]->{handles} }
28 sub weak_ref { $_[0]->{weak_ref} }
29 sub init_arg { $_[0]->{init_arg} }
30 sub type_constraint { $_[0]->{type_constraint} }
31 sub trigger { $_[0]->{trigger} }
32 sub builder { $_[0]->{builder} }
34 sub has_default { exists $_[0]->{default} }
35 sub has_predicate { exists $_[0]->{predicate} }
36 sub has_clearer { exists $_[0]->{clearer} }
37 sub has_handles { exists $_[0]->{handles} }
38 sub has_weak_ref { exists $_[0]->{weak_ref} }
39 sub has_init_arg { exists $_[0]->{init_arg} }
40 sub has_type_constraint { exists $_[0]->{type_constraint} }
41 sub has_trigger { exists $_[0]->{trigger} }
42 sub has_builder { exists $_[0]->{builder} }
44 sub generate_accessor {
45 my $attribute = shift;
47 my $name = $attribute->name;
48 my $key = $attribute->init_arg;
49 my $default = $attribute->default;
50 my $trigger = $attribute->trigger;
51 my $type = $attribute->type_constraint;
52 my $constraint = $attribute->find_type_constraint;
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} = ';
85 $accessor .= ref($default) eq 'CODE'
88 $accessor .= ' if !exists($self->{$key});';
91 $accessor .= 'return $self->{$key}
94 return eval $accessor;
97 sub generate_predicate {
98 my $attribute = shift;
99 my $key = $attribute->init_arg;
101 my $predicate = 'sub { exists($_[0]->{$key}) }';
103 return eval $predicate;
106 sub generate_clearer {
107 my $attribute = shift;
108 my $key = $attribute->init_arg;
110 my $predicate = 'sub { delete($_[0]->{$key}) }';
112 return eval $predicate;
115 sub generate_handles {
116 my $attribute = shift;
117 my $reader = $attribute->name;
121 for my $local_method (keys %{ $attribute->handles }) {
122 my $remote_method = $attribute->handles->{$local_method};
126 $self->$reader->$remote_method(@_)
129 $method_map{$local_method} = eval $method;
136 my ($self, $class, $name, %args) = @_;
138 confess "You must specify a default for lazy attribute '$name'"
139 if $args{lazy} && !exists($args{default});
141 confess "Trigger is not allowed on read-only attribute '$name'"
142 if $args{trigger} && $args{is} ne 'rw';
144 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
145 if ref($args{default})
146 && ref($args{default}) ne 'CODE';
148 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
150 && ref($args{handles}) eq 'ARRAY';
152 confess "You must pass a HASH or ARRAY to handles"
153 if exists($args{handles})
154 && ref($args{handles}) ne 'HASH';
156 $args{type_constraint} = delete $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 confess "Unable to parse type constraint '$type'";
203 sub verify_type_constraint {
207 my $type = $self->type_constraint
209 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 $_");
225 Mouse::Attribute - attribute metaclass
229 =head2 new %args -> Mouse::Attribute
231 Instantiates a new Mouse::Attribute. Does nothing else.
233 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
235 Creates a new attribute in OwnerClass. Accessors and helper methods are
236 installed. Some error checking is done.
238 =head2 name -> AttributeName
240 =head2 class -> OwnerClass
242 =head2 default -> Value
244 =head2 predicate -> MethodName
246 =head2 clearer -> MethodName
248 =head2 handles -> { LocalName => RemoteName }
250 =head2 weak_ref -> Bool
252 =head2 init_arg -> Str
254 Informational methods.
256 =head2 generate_accessor -> CODE
258 Creates a new code reference for the attribute's accessor.
260 =head2 generate_predicate -> CODE
262 Creates a new code reference for the attribute's predicate.
264 =head2 generate_clearer -> CODE
266 Creates a new code reference for the attribute's clearer.
268 =head2 generate_handles -> { MethodName => CODE }
270 Creates a new code reference for each of the attribute's handles methods.