1 package Mouse::Meta::Attribute;
7 use Mouse::Meta::TypeConstraint;
8 use Mouse::Meta::Method::Accessor;
11 my ($class, $name, %options) = @_;
13 $options{name} = $name;
15 $options{init_arg} = $name
16 unless exists $options{init_arg};
18 my $is = $options{is} ||= '';
21 $options{accessor} = $name if !exists $options{accessor};
24 $options{reader} = $name if !exists $options{reader};
27 bless \%options, $class;
32 sub name { $_[0]->{name} }
33 sub associated_class { $_[0]->{associated_class} }
35 sub accessor { $_[0]->{accessor} }
36 sub reader { $_[0]->{reader} }
37 sub writer { $_[0]->{writer} }
38 sub predicate { $_[0]->{predicate} }
39 sub clearer { $_[0]->{clearer} }
40 sub handles { $_[0]->{handles} }
42 sub _is_metadata { $_[0]->{is} }
43 sub is_required { $_[0]->{required} }
44 sub default { $_[0]->{default} }
45 sub is_lazy { $_[0]->{lazy} }
46 sub is_lazy_build { $_[0]->{lazy_build} }
47 sub is_weak_ref { $_[0]->{weak_ref} }
48 sub init_arg { $_[0]->{init_arg} }
49 sub type_constraint { $_[0]->{type_constraint} }
50 sub find_type_constraint {
51 Carp::carp("This method was deprecated");
52 $_[0]->type_constraint();
54 sub trigger { $_[0]->{trigger} }
55 sub builder { $_[0]->{builder} }
56 sub should_auto_deref { $_[0]->{auto_deref} }
57 sub should_coerce { $_[0]->{should_coerce} }
61 sub has_accessor { exists $_[0]->{accessor} }
62 sub has_reader { exists $_[0]->{reader} }
63 sub has_writer { exists $_[0]->{writer} }
64 sub has_predicate { exists $_[0]->{predicate} }
65 sub has_clearer { exists $_[0]->{clearer} }
66 sub has_handles { exists $_[0]->{handles} }
68 sub has_default { exists $_[0]->{default} }
69 sub has_type_constraint { exists $_[0]->{type_constraint} }
70 sub has_trigger { exists $_[0]->{trigger} }
71 sub has_builder { exists $_[0]->{builder} }
74 $_[0]->{_create_args} = $_[1] if @_ > 1;
78 sub accessor_metaclass { 'Mouse::Meta::Method::Accessor' }
81 my ($self, $class, $name, %args) = @_;
84 $args{associated_class} = $class;
86 %args = $self->canonicalize_args($name, %args);
87 $self->validate_args($name, \%args);
89 $args{should_coerce} = delete $args{coerce}
90 if exists $args{coerce};
92 if (exists $args{isa}) {
93 my $type_constraint = delete $args{isa};
94 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
97 my $attribute = $self->new($name, %args);
99 $attribute->_create_args(\%args);
101 $class->add_attribute($attribute);
103 my $associated_methods = 0;
105 my $generator_class = $self->accessor_metaclass;
106 foreach my $type(qw(accessor reader writer predicate clearer handles)){
107 if(exists $attribute->{$type}){
108 my $installer = '_install_' . $type;
109 $generator_class->$installer($attribute, $attribute->{$type}, $class);
110 $associated_methods++;
114 if($associated_methods == 0 && ($attribute->_is_metadata || '') ne 'bare'){
115 Carp::cluck(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
122 sub canonicalize_args {
127 if ($args{lazy_build}) {
130 $args{builder} = "_build_${name}"
131 if !exists($args{builder});
133 $args{clearer} = "_clear${name}" if !exists($args{clearer});
134 $args{predicate} = "_has${name}" if !exists($args{predicate});
137 $args{clearer} = "clear_${name}" if !exists($args{clearer});
138 $args{predicate} = "has_${name}" if !exists($args{predicate});
150 $self->throw_error("You can not use lazy_build and default for the same attribute ($name)")
151 if $args->{lazy_build} && exists $args->{default};
153 $self->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it")
155 && !exists($args->{default})
156 && !exists($args->{builder});
158 $self->throw_error("References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
159 if ref($args->{default})
160 && ref($args->{default}) ne 'CODE';
162 $self->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)")
163 if $args->{auto_deref} && !exists($args->{isa});
165 $self->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)")
166 if $args->{auto_deref}
167 && $args->{isa} !~ /^(?:ArrayRef|HashRef)(?:\[.*\])?$/;
169 if ($args->{trigger}) {
170 if (ref($args->{trigger}) eq 'HASH') {
171 $self->throw_error("HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.");
174 $self->throw_error("Trigger must be a CODE ref on attribute ($name)")
175 if ref($args->{trigger}) ne 'CODE';
181 sub verify_against_type_constraint {
182 my ($self, $value) = @_;
183 my $tc = $self->type_constraint;
187 return 1 if $tc->check($value);
189 $self->verify_type_constraint_error($self->name, $value, $tc);
192 sub verify_type_constraint_error {
193 my($self, $name, $value, $type) = @_;
194 $self->throw_error("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
197 sub coerce_constraint { ## my($self, $value) = @_;
198 my $type = $_[0]->{type_constraint}
200 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
203 sub _canonicalize_handles {
207 if (ref($handles) eq 'HASH') {
210 elsif (ref($handles) eq 'ARRAY') {
211 return map { $_ => $_ } @$handles;
214 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
222 my %args = ($self->get_parent_args($class, $name), @_);
224 $self->create($class, $name, %args);
227 sub get_parent_args {
232 for my $super ($class->linearized_isa) {
233 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
235 return %{ $super_attr->_create_args };
238 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
244 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
245 $metaclass->throw_error(@_, depth => 1);
254 Mouse::Meta::Attribute - attribute metaclass
258 =head2 new %args -> Mouse::Meta::Attribute
260 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
262 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
264 Creates a new attribute in OwnerClass. Accessors and helper methods are
265 installed. Some error checking is done.
267 =head2 name -> AttributeName
269 =head2 associated_class -> OwnerClass
271 =head2 is_required -> Bool
273 =head2 default -> Item
275 =head2 has_default -> Bool
277 =head2 is_lazy -> Bool
279 =head2 predicate -> MethodName | Undef
281 =head2 has_predicate -> Bool
283 =head2 clearer -> MethodName | Undef
285 =head2 has_clearer -> Bool
287 =head2 handles -> { LocalName => RemoteName }
289 =head2 has_handles -> Bool
291 =head2 is_weak_ref -> Bool
293 =head2 init_arg -> Str
295 =head2 type_constraint -> Str
297 =head2 has_type_constraint -> Bool
299 =head2 trigger => CODE | Undef
301 =head2 has_trigger -> Bool
303 =head2 builder => MethodName | Undef
305 =head2 has_builder -> Bool
307 =head2 is_lazy_build => Bool
309 =head2 should_auto_deref -> Bool
311 Informational methods.
313 =head2 verify_against_type_constraint Item -> 1 | ERROR
315 Checks that the given value passes this attribute's type constraint. Returns 1
316 on success, otherwise C<confess>es.
318 =head2 canonicalize_args Name, %args -> %args
320 Canonicalizes some arguments to create. In particular, C<lazy_build> is
321 canonicalized into C<lazy>, C<builder>, etc.
323 =head2 validate_args Name, \%args -> 1 | ERROR
325 Checks that the arguments to create the attribute (ie those specified by
328 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
330 Creates a new attribute in OwnerClass, inheriting options from parent classes.
331 Accessors and helper methods are installed. Some error checking is done.
333 =head2 get_parent_args OwnerClass, AttributeName -> Hash
335 Returns the options that the parent class of C<OwnerClass> used for attribute