1 package Mouse::Meta::Attribute;
5 use Mouse::Meta::TypeConstraint;
6 use Mouse::Meta::Method::Accessor;
9 my ($class, $name, %options) = @_;
11 $options{name} = $name;
13 $options{init_arg} = $name
14 unless exists $options{init_arg};
16 my $is = $options{is} ||= '';
19 $options{accessor} = $name if !exists $options{accessor};
22 $options{reader} = $name if !exists $options{reader};
25 bless \%options, $class;
30 sub name { $_[0]->{name} }
31 sub associated_class { $_[0]->{associated_class} }
33 sub accessor { $_[0]->{accessor} }
34 sub reader { $_[0]->{reader} }
35 sub writer { $_[0]->{writer} }
36 sub predicate { $_[0]->{predicate} }
37 sub clearer { $_[0]->{clearer} }
38 sub handles { $_[0]->{handles} }
40 sub _is_metadata { $_[0]->{is} }
41 sub is_required { $_[0]->{required} }
42 sub default { $_[0]->{default} }
43 sub is_lazy { $_[0]->{lazy} }
44 sub is_lazy_build { $_[0]->{lazy_build} }
45 sub is_weak_ref { $_[0]->{weak_ref} }
46 sub init_arg { $_[0]->{init_arg} }
47 sub type_constraint { $_[0]->{type_constraint} }
48 sub find_type_constraint {
49 Carp::carp("This method was deprecated");
50 $_[0]->type_constraint();
52 sub trigger { $_[0]->{trigger} }
53 sub builder { $_[0]->{builder} }
54 sub should_auto_deref { $_[0]->{auto_deref} }
55 sub should_coerce { $_[0]->{should_coerce} }
59 sub has_accessor { exists $_[0]->{accessor} }
60 sub has_reader { exists $_[0]->{reader} }
61 sub has_writer { exists $_[0]->{writer} }
62 sub has_predicate { exists $_[0]->{predicate} }
63 sub has_clearer { exists $_[0]->{clearer} }
64 sub has_handles { exists $_[0]->{handles} }
66 sub has_default { exists $_[0]->{default} }
67 sub has_type_constraint { exists $_[0]->{type_constraint} }
68 sub has_trigger { exists $_[0]->{trigger} }
69 sub has_builder { exists $_[0]->{builder} }
72 $_[0]->{_create_args} = $_[1] if @_ > 1;
76 sub accessor_metaclass { 'Mouse::Meta::Method::Accessor' }
79 my ($self, $class, $name, %args) = @_;
82 $args{associated_class} = $class;
84 %args = $self->canonicalize_args($name, %args);
85 $self->validate_args($name, \%args);
87 $args{should_coerce} = delete $args{coerce}
88 if exists $args{coerce};
90 if (exists $args{isa}) {
91 my $type_constraint = delete $args{isa};
92 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
95 my $attribute = $self->new($name, %args);
97 $attribute->_create_args(\%args);
99 $class->add_attribute($attribute);
101 my $associated_methods = 0;
103 my $generator_class = $self->accessor_metaclass;
104 foreach my $type(qw(accessor reader writer predicate clearer handles)){
105 if(exists $attribute->{$type}){
106 my $installer = '_install_' . $type;
107 $generator_class->$installer($attribute, $attribute->{$type}, $class);
108 $associated_methods++;
112 if($associated_methods == 0 && ($attribute->_is_metadata || '') ne 'bare'){
113 Carp::cluck(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
120 sub canonicalize_args {
125 if ($args{lazy_build}) {
128 $args{builder} = "_build_${name}"
129 if !exists($args{builder});
131 $args{clearer} = "_clear${name}" if !exists($args{clearer});
132 $args{predicate} = "_has${name}" if !exists($args{predicate});
135 $args{clearer} = "clear_${name}" if !exists($args{clearer});
136 $args{predicate} = "has_${name}" if !exists($args{predicate});
148 $self->throw_error("You can not use lazy_build and default for the same attribute ($name)")
149 if $args->{lazy_build} && exists $args->{default};
151 $self->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it")
153 && !exists($args->{default})
154 && !exists($args->{builder});
156 $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 [])")
157 if ref($args->{default})
158 && ref($args->{default}) ne 'CODE';
160 $self->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)")
161 if $args->{auto_deref} && !exists($args->{isa});
163 $self->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)")
164 if $args->{auto_deref}
165 && $args->{isa} !~ /^(?:ArrayRef|HashRef)(?:\[.*\])?$/;
167 if ($args->{trigger}) {
168 if (ref($args->{trigger}) eq 'HASH') {
169 $self->throw_error("HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.");
172 $self->throw_error("Trigger must be a CODE ref on attribute ($name)")
173 if ref($args->{trigger}) ne 'CODE';
179 sub verify_against_type_constraint {
180 my ($self, $value) = @_;
181 my $tc = $self->type_constraint;
185 return 1 if $tc->check($value);
187 $self->verify_type_constraint_error($self->name, $value, $tc);
190 sub verify_type_constraint_error {
191 my($self, $name, $value, $type) = @_;
192 $self->throw_error("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
195 sub coerce_constraint { ## my($self, $value) = @_;
196 my $type = $_[0]->{type_constraint}
198 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
201 sub _canonicalize_handles {
205 if (ref($handles) eq 'HASH') {
208 elsif (ref($handles) eq 'ARRAY') {
209 return map { $_ => $_ } @$handles;
212 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
220 my %args = ($self->get_parent_args($class, $name), @_);
222 $self->create($class, $name, %args);
225 sub get_parent_args {
230 for my $super ($class->linearized_isa) {
231 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
233 return %{ $super_attr->_create_args };
236 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
242 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
243 $metaclass->throw_error(@_, depth => 1);
252 Mouse::Meta::Attribute - attribute metaclass
256 =head2 new %args -> Mouse::Meta::Attribute
258 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
260 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
262 Creates a new attribute in OwnerClass. Accessors and helper methods are
263 installed. Some error checking is done.
265 =head2 name -> AttributeName
267 =head2 associated_class -> OwnerClass
269 =head2 is_required -> Bool
271 =head2 default -> Item
273 =head2 has_default -> Bool
275 =head2 is_lazy -> Bool
277 =head2 predicate -> MethodName | Undef
279 =head2 has_predicate -> Bool
281 =head2 clearer -> MethodName | Undef
283 =head2 has_clearer -> Bool
285 =head2 handles -> { LocalName => RemoteName }
287 =head2 has_handles -> Bool
289 =head2 is_weak_ref -> Bool
291 =head2 init_arg -> Str
293 =head2 type_constraint -> Str
295 =head2 has_type_constraint -> Bool
297 =head2 trigger => CODE | Undef
299 =head2 has_trigger -> Bool
301 =head2 builder => MethodName | Undef
303 =head2 has_builder -> Bool
305 =head2 is_lazy_build => Bool
307 =head2 should_auto_deref -> Bool
309 Informational methods.
311 =head2 verify_against_type_constraint Item -> 1 | ERROR
313 Checks that the given value passes this attribute's type constraint. Returns 1
314 on success, otherwise C<confess>es.
316 =head2 canonicalize_args Name, %args -> %args
318 Canonicalizes some arguments to create. In particular, C<lazy_build> is
319 canonicalized into C<lazy>, C<builder>, etc.
321 =head2 validate_args Name, \%args -> 1 | ERROR
323 Checks that the arguments to create the attribute (ie those specified by
326 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
328 Creates a new attribute in OwnerClass, inheriting options from parent classes.
329 Accessors and helper methods are installed. Some error checking is done.
331 =head2 get_parent_args OwnerClass, AttributeName -> Hash
333 Returns the options that the parent class of C<OwnerClass> used for attribute