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 default { $_[0]->{default} }
22 sub predicate { $_[0]->{predicate} }
23 sub clearer { $_[0]->{clearer} }
24 sub handles { $_[0]->{handles} }
25 sub weak_ref { $_[0]->{weak_ref} }
26 sub init_arg { $_[0]->{init_arg} }
27 sub type_constraint { $_[0]->{type_constraint} }
29 sub has_name { exists $_[0]->{name} }
30 sub has_class { exists $_[0]->{class} }
31 sub has_default { exists $_[0]->{default} }
32 sub has_predicate { exists $_[0]->{predicate} }
33 sub has_clearer { exists $_[0]->{clearer} }
34 sub has_handles { exists $_[0]->{handles} }
35 sub has_weak_ref { exists $_[0]->{weak_ref} }
36 sub has_init_arg { exists $_[0]->{init_arg} }
37 sub has_type_constraint { exists $_[0]->{type_constraint} }
39 sub generate_accessor {
40 my $attribute = shift;
42 my $name = $attribute->{name};
43 my $key = $attribute->{init_arg};
44 my $default = $attribute->{default};
45 my $trigger = $attribute->{trigger};
46 my $type = $attribute->{type_constraint};
47 my $constraint = $attribute->find_type_constraint;
52 if ($attribute->{is} eq 'rw') {
53 $accessor .= 'if (@_) {
57 $accessor .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") unless $constraint->();'
60 $accessor .= '$self->{$key} = $_;';
62 if ($attribute->{weak_ref}) {
63 $accessor .= 'Scalar::Util::weaken($self->{$key});';
67 $accessor .= '$trigger->($self, $_, $attribute);';
75 if ($attribute->{lazy}) {
76 $accessor .= '$self->{$key} = ';
77 $accessor .= ref($attribute->{default}) eq 'CODE'
80 $accessor .= ' if !exists($self->{$key});';
83 $accessor .= 'return $self->{$key}
86 return eval $accessor;
89 sub generate_predicate {
90 my $attribute = shift;
91 my $key = $attribute->{init_arg};
93 my $predicate = 'sub { exists($_[0]->{$key}) }';
95 return eval $predicate;
98 sub generate_clearer {
99 my $attribute = shift;
100 my $key = $attribute->{init_arg};
102 my $predicate = 'sub { delete($_[0]->{$key}) }';
104 return eval $predicate;
107 sub generate_handles {
108 my $attribute = shift;
109 my $reader = $attribute->{name};
113 for my $local_method (keys %{ $attribute->{handles} }) {
114 my $remote_method = $attribute->{handles}{$local_method};
118 $self->$reader->$remote_method(@_)
121 $method_map{$local_method} = eval $method;
128 my ($self, $class, $name, %args) = @_;
130 confess "You must specify a default for lazy attribute '$name'"
131 if $args{lazy} && !exists($args{default});
133 confess "Trigger is not allowed on read-only attribute '$name'"
134 if $args{trigger} && $args{is} ne 'rw';
136 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
137 if ref($args{default})
138 && ref($args{default}) ne 'CODE';
140 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
142 && ref($args{handles}) eq 'ARRAY';
144 confess "You must pass a HASH or ARRAY to handles"
145 if exists($args{handles})
146 && ref($args{handles}) ne 'HASH';
148 $args{type_constraint} = delete $args{isa};
150 my $attribute = $self->new(%args, name => $name, class => $class);
151 my $meta = $class->meta;
153 $meta->add_attribute($attribute);
155 # install an accessor
156 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
157 my $accessor = $attribute->generate_accessor;
159 *{ $class . '::' . $name } = $accessor;
162 for my $method (qw/predicate clearer/) {
163 if (exists $attribute->{$method}) {
164 my $generator = "generate_$method";
165 my $coderef = $attribute->$generator;
167 *{ $class . '::' . $attribute->{$method} } = $coderef;
171 if ($attribute->{handles}) {
172 my $method_map = $attribute->generate_handles;
173 for my $method_name (keys %$method_map) {
175 *{ $class . '::' . $method_name } = $method_map->{$method_name};
182 sub find_type_constraint {
184 my $type = $self->type_constraint;
188 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
189 return $checker if $checker;
191 confess "Unable to parse type constraint '$type'";
194 sub verify_type_constraint {
198 my $type = $self->type_constraint
200 my $constraint = $self->find_type_constraint
203 return 1 if $constraint->($_);
205 my $name = $self->name;
206 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
215 Mouse::Attribute - attribute metaclass
219 =head2 new %args -> Mouse::Attribute
221 Instantiates a new Mouse::Attribute. Does nothing else.
223 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
225 Creates a new attribute in OwnerClass. Accessors and helper methods are
226 installed. Some error checking is done.
228 =head2 name -> AttributeName
230 =head2 class -> OwnerClass
232 =head2 default -> Value
234 =head2 predicate -> MethodName
236 =head2 clearer -> MethodName
238 =head2 handles -> { LocalName => RemoteName }
240 =head2 weak_ref -> Bool
242 =head2 init_arg -> Str
244 Informational methods.
246 =head2 generate_accessor -> CODE
248 Creates a new code reference for the attribute's accessor.
250 =head2 generate_predicate -> CODE
252 Creates a new code reference for the attribute's predicate.
254 =head2 generate_clearer -> CODE
256 Creates a new code reference for the attribute's clearer.
258 =head2 generate_handles -> { MethodName => CODE }
260 Creates a new code reference for each of the attribute's handles methods.