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};
48 my $constraint = sub {
51 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
52 return $checker if $checker;
54 confess "Unable to parse type constraint '$type'";
60 if ($attribute->{is} eq 'rw') {
61 $accessor .= 'if (@_) {
65 $accessor .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") 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->{lazy}) {
84 $accessor .= '$self->{$key} = ';
85 $accessor .= ref($attribute->{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} eq 'rw' || $attribute->{is} eq 'ro') {
165 my $accessor = $attribute->generate_accessor;
167 *{ $class . '::' . $name } = $accessor;
170 for my $method (qw/predicate clearer/) {
171 if (exists $attribute->{$method}) {
172 my $generator = "generate_$method";
173 my $coderef = $attribute->$generator;
175 *{ $class . '::' . $attribute->{$method} } = $coderef;
179 if ($attribute->{handles}) {
180 my $method_map = $attribute->generate_handles;
181 for my $method_name (keys %$method_map) {
183 *{ $class . '::' . $method_name } = $method_map->{$method_name};
196 Mouse::Attribute - attribute metaclass
200 =head2 new %args -> Mouse::Attribute
202 Instantiates a new Mouse::Attribute. Does nothing else.
204 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
206 Creates a new attribute in OwnerClass. Accessors and helper methods are
207 installed. Some error checking is done.
209 =head2 name -> AttributeName
211 =head2 class -> OwnerClass
213 =head2 default -> Value
215 =head2 predicate -> MethodName
217 =head2 clearer -> MethodName
219 =head2 handles -> { LocalName => RemoteName }
221 =head2 weak_ref -> Bool
223 =head2 init_arg -> Str
225 Informational methods.
227 =head2 generate_accessor -> CODE
229 Creates a new code reference for the attribute's accessor.
231 =head2 generate_predicate -> CODE
233 Creates a new code reference for the attribute's predicate.
235 =head2 generate_clearer -> CODE
237 Creates a new code reference for the attribute's clearer.
239 =head2 generate_handles -> { MethodName => CODE }
241 Creates a new code reference for each of the attribute's handles methods.