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 required { $_[0]->{required} }
22 sub default { $_[0]->{default} }
23 sub lazy { $_[0]->{lazy} }
24 sub predicate { $_[0]->{predicate} }
25 sub clearer { $_[0]->{clearer} }
26 sub handles { $_[0]->{handles} }
27 sub weak_ref { $_[0]->{weak_ref} }
28 sub init_arg { $_[0]->{init_arg} }
29 sub type_constraint { $_[0]->{type_constraint} }
30 sub trigger { $_[0]->{trigger} }
31 sub builder { $_[0]->{builder} }
33 sub has_default { exists $_[0]->{default} }
34 sub has_predicate { exists $_[0]->{predicate} }
35 sub has_clearer { exists $_[0]->{clearer} }
36 sub has_handles { exists $_[0]->{handles} }
37 sub has_weak_ref { exists $_[0]->{weak_ref} }
38 sub has_init_arg { exists $_[0]->{init_arg} }
39 sub has_type_constraint { exists $_[0]->{type_constraint} }
40 sub has_trigger { exists $_[0]->{trigger} }
41 sub has_builder { exists $_[0]->{builder} }
43 sub generate_accessor {
44 my $attribute = shift;
46 my $name = $attribute->{name};
47 my $key = $attribute->{init_arg};
48 my $default = $attribute->{default};
49 my $trigger = $attribute->{trigger};
50 my $type = $attribute->{type_constraint};
51 my $constraint = $attribute->find_type_constraint;
56 if ($attribute->{is} eq 'rw') {
57 $accessor .= 'if (@_) {
62 my $display = defined($_) ? $_ : "undef";
63 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
67 $accessor .= '$self->{$key} = $_;';
69 if ($attribute->{weak_ref}) {
70 $accessor .= 'Scalar::Util::weaken($self->{$key});';
74 $accessor .= '$trigger->($self, $_, $attribute);';
82 if ($attribute->{lazy}) {
83 $accessor .= '$self->{$key} = ';
84 $accessor .= ref($attribute->{default}) eq 'CODE'
87 $accessor .= ' if !exists($self->{$key});';
90 $accessor .= 'return $self->{$key}
93 return eval $accessor;
96 sub generate_predicate {
97 my $attribute = shift;
98 my $key = $attribute->{init_arg};
100 my $predicate = 'sub { exists($_[0]->{$key}) }';
102 return eval $predicate;
105 sub generate_clearer {
106 my $attribute = shift;
107 my $key = $attribute->{init_arg};
109 my $predicate = 'sub { delete($_[0]->{$key}) }';
111 return eval $predicate;
114 sub generate_handles {
115 my $attribute = shift;
116 my $reader = $attribute->{name};
120 for my $local_method (keys %{ $attribute->{handles} }) {
121 my $remote_method = $attribute->{handles}{$local_method};
125 $self->$reader->$remote_method(@_)
128 $method_map{$local_method} = eval $method;
135 my ($self, $class, $name, %args) = @_;
137 confess "You must specify a default for lazy attribute '$name'"
138 if $args{lazy} && !exists($args{default});
140 confess "Trigger is not allowed on read-only attribute '$name'"
141 if $args{trigger} && $args{is} ne 'rw';
143 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
144 if ref($args{default})
145 && ref($args{default}) ne 'CODE';
147 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
149 && ref($args{handles}) eq 'ARRAY';
151 confess "You must pass a HASH or ARRAY to handles"
152 if exists($args{handles})
153 && ref($args{handles}) ne 'HASH';
155 $args{type_constraint} = delete $args{isa};
157 my $attribute = $self->new(%args, name => $name, class => $class);
158 my $meta = $class->meta;
160 $meta->add_attribute($attribute);
162 # install an accessor
163 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
164 my $accessor = $attribute->generate_accessor;
166 *{ $class . '::' . $name } = $accessor;
169 for my $method (qw/predicate clearer/) {
170 if (exists $attribute->{$method}) {
171 my $generator = "generate_$method";
172 my $coderef = $attribute->$generator;
174 *{ $class . '::' . $attribute->{$method} } = $coderef;
178 if ($attribute->{handles}) {
179 my $method_map = $attribute->generate_handles;
180 for my $method_name (keys %$method_map) {
182 *{ $class . '::' . $method_name } = $method_map->{$method_name};
189 sub find_type_constraint {
191 my $type = $self->type_constraint;
195 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
196 return $checker if $checker;
198 confess "Unable to parse type constraint '$type'";
201 sub verify_type_constraint {
205 my $type = $self->type_constraint
207 my $constraint = $self->find_type_constraint
210 return 1 if $constraint->($_);
212 my $name = $self->name;
213 local $_ = "undef" unless defined($_);
214 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
223 Mouse::Attribute - attribute metaclass
227 =head2 new %args -> Mouse::Attribute
229 Instantiates a new Mouse::Attribute. Does nothing else.
231 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
233 Creates a new attribute in OwnerClass. Accessors and helper methods are
234 installed. Some error checking is done.
236 =head2 name -> AttributeName
238 =head2 class -> OwnerClass
240 =head2 default -> Value
242 =head2 predicate -> MethodName
244 =head2 clearer -> MethodName
246 =head2 handles -> { LocalName => RemoteName }
248 =head2 weak_ref -> Bool
250 =head2 init_arg -> Str
252 Informational methods.
254 =head2 generate_accessor -> CODE
256 Creates a new code reference for the attribute's accessor.
258 =head2 generate_predicate -> CODE
260 Creates a new code reference for the attribute's predicate.
262 =head2 generate_clearer -> CODE
264 Creates a new code reference for the attribute's clearer.
266 =head2 generate_handles -> { MethodName => CODE }
268 Creates a new code reference for each of the attribute's handles methods.