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 generate_accessor {
30 my $attribute = shift;
32 my $name = $attribute->{name};
33 my $key = $attribute->{init_arg};
34 my $default = $attribute->{default};
35 my $trigger = $attribute->{trigger};
36 my $type = $attribute->{type_constraint};
38 my $constraint = sub {
41 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
42 return $checker if $checker;
44 confess "Unable to parse type constraint '$type'";
50 if ($attribute->{is} eq 'rw') {
51 $accessor .= 'if (@_) {
55 $accessor .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") unless $constraint->();'
58 $accessor .= '$self->{$key} = $_;';
60 if ($attribute->{weak_ref}) {
61 $accessor .= 'Scalar::Util::weaken($self->{$key});';
65 $accessor .= '$trigger->($self, $_, $attribute);';
73 if ($attribute->{lazy}) {
74 $accessor .= '$self->{$key} = ';
75 $accessor .= ref($attribute->{default}) eq 'CODE'
78 $accessor .= ' if !exists($self->{$key});';
81 $accessor .= 'return $self->{$key}
84 return eval $accessor;
87 sub generate_predicate {
88 my $attribute = shift;
89 my $key = $attribute->{init_arg};
91 my $predicate = 'sub { exists($_[0]->{$key}) }';
93 return eval $predicate;
96 sub generate_clearer {
97 my $attribute = shift;
98 my $key = $attribute->{init_arg};
100 my $predicate = 'sub { delete($_[0]->{$key}) }';
102 return eval $predicate;
105 sub generate_handles {
106 my $attribute = shift;
107 my $reader = $attribute->{name};
111 for my $local_method (keys %{ $attribute->{handles} }) {
112 my $remote_method = $attribute->{handles}{$local_method};
116 $self->$reader->$remote_method(@_)
119 $method_map{$local_method} = eval $method;
126 my ($self, $class, $name, %args) = @_;
128 confess "You must specify a default for lazy attribute '$name'"
129 if $args{lazy} && !exists($args{default});
131 confess "Trigger is not allowed on read-only attribute '$name'"
132 if $args{trigger} && $args{is} ne 'rw';
134 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
135 if ref($args{default})
136 && ref($args{default}) ne 'CODE';
138 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
140 && ref($args{handles}) eq 'ARRAY';
142 confess "You must pass a HASH or ARRAY to handles"
143 if exists($args{handles})
144 && ref($args{handles}) ne 'HASH';
146 $args{type_constraint} = delete $args{isa};
148 my $attribute = $self->new(%args, name => $name, class => $class);
149 my $meta = $class->meta;
151 $meta->add_attribute($attribute);
153 # install an accessor
154 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
155 my $accessor = $attribute->generate_accessor;
157 *{ $class . '::' . $name } = $accessor;
160 for my $method (qw/predicate clearer/) {
161 if (exists $attribute->{$method}) {
162 my $generator = "generate_$method";
163 my $coderef = $attribute->$generator;
165 *{ $class . '::' . $attribute->{$method} } = $coderef;
169 if ($attribute->{handles}) {
170 my $method_map = $attribute->generate_handles;
171 for my $method_name (keys %$method_map) {
173 *{ $class . '::' . $method_name } = $method_map->{$method_name};
186 Mouse::Attribute - attribute metaclass
190 =head2 new %args -> Mouse::Attribute
192 Instantiates a new Mouse::Attribute. Does nothing else.
194 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
196 Creates a new attribute in OwnerClass. Accessors and helper methods are
197 installed. Some error checking is done.
199 =head2 name -> AttributeName
201 =head2 class -> OwnerClass
203 =head2 default -> Value
205 =head2 predicate -> MethodName
207 =head2 clearer -> MethodName
209 =head2 handles -> { LocalName => RemoteName }
211 =head2 weak_ref -> Bool
213 =head2 init_arg -> Str
215 Informational methods.
217 =head2 generate_accessor -> CODE
219 Creates a new code reference for the attribute's accessor.
221 =head2 generate_predicate -> CODE
223 Creates a new code reference for the attribute's predicate.
225 =head2 generate_clearer -> CODE
227 Creates a new code reference for the attribute's clearer.
229 =head2 generate_handles -> { MethodName => CODE }
231 Creates a new code reference for each of the attribute's handles methods.