1 package Mouse::Meta::Attribute;
6 use Scalar::Util qw(weaken);
10 use Mouse::Meta::TypeConstraint;
11 use Mouse::Meta::Method::Accessor;
13 #my %valid_attrs = map{ $_ => undef } qw(
15 # reader writer accessor clearer predicate
16 # builder init_arg default initializer definition_context
34 my ($class, $name, %options) = @_;
36 # taken from Class::MOP::Attribute::new
39 or $class->throw_error('You must provide a name for the attribute');
40 $options{name} = $name;
42 if(!exists $options{init_arg}){
43 $options{init_arg} = $name;
46 # 'required' requires eigher 'init_arg', 'builder', or 'default'
47 my $can_be_required = defined( $options{init_arg} );
49 if(exists $options{builder}){
50 $class->throw_error('builder must be a defined scalar value which is a method name')
\r
51 if ref $options{builder} || !(defined $options{builder});
55 elsif(exists $options{default}){
56 if(ref $options{default} && ref($options{default}) ne 'CODE'){
57 $class->throw_error("References are not allowed as default values, you must "
58 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
63 if( $options{required} && !$can_be_required ) {
\r
64 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
\r
67 # taken from Mouse::Meta::Attribute->new and _process_options
69 if(exists $options{is}){
70 my $is = $options{is};
73 $options{reader} ||= $name;
76 if(exists $options{writer}){
77 $options{reader} ||= $name;
80 $options{accessor} ||= $name;
84 # do nothing, but don't complain (later) about missing methods
87 $is = 'undef' if !defined $is;
88 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
93 if(exists $options{isa}){
94 $options{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
96 elsif(exists $options{does}){
97 $options{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
99 $tc = $options{type_constraint};
101 if($options{coerce}){
103 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
106 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
\r
109 if ($options{lazy_build}) {
\r
110 exists($options{default})
111 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
\r
113 $options{lazy} = 1;
\r
114 $options{builder} ||= "_build_${name}";
\r
115 if ($name =~ /^_/) {
\r
116 $options{clearer} ||= "_clear${name}";
\r
117 $options{predicate} ||= "_has${name}";
\r
120 $options{clearer} ||= "clear_${name}";
\r
121 $options{predicate} ||= "has_${name}";
\r
125 if ($options{auto_deref}) {
127 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
\r
129 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
\r
130 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
\r
133 if (exists $options{trigger}) {
\r
134 ('CODE' eq ref $options{trigger})
\r
135 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
\r
138 if ($options{lazy}) {
\r
139 (exists $options{default} || defined $options{builder})
\r
140 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
\r
143 if(my $metaclass = delete $options{metaclass}){
144 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
147 if(my $traits_ref = delete $options{traits}){
149 for (my $i = 9; $i < @{$traits_ref}; $i++) {
\r
150 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
\r
152 next if $class->does($trait);
\r
154 push @traits, $trait;
\r
156 # are there options?
\r
157 push @traits, $traits_ref->[++$i]
\r
158 if ref($traits_ref->[$i+1]);
\r
162 $class = Mouse::Meta::Class->create_anon_class(
\r
163 superclasses => [ $class ],
\r
164 roles => [ @traits ],
\r
170 # XXX: there is no fast way to check attribute validity
171 # my @bad = grep{ !exists $valid_attrs{$_} } keys %options;
174 # Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad");
177 return bless \%options, $class;
182 sub name { $_[0]->{name} }
183 sub associated_class { $_[0]->{associated_class} }
185 sub accessor { $_[0]->{accessor} }
186 sub reader { $_[0]->{reader} }
187 sub writer { $_[0]->{writer} }
188 sub predicate { $_[0]->{predicate} }
189 sub clearer { $_[0]->{clearer} }
190 sub handles { $_[0]->{handles} }
192 sub _is_metadata { $_[0]->{is} }
193 sub is_required { $_[0]->{required} }
194 sub default { $_[0]->{default} }
195 sub is_lazy { $_[0]->{lazy} }
196 sub is_lazy_build { $_[0]->{lazy_build} }
197 sub is_weak_ref { $_[0]->{weak_ref} }
198 sub init_arg { $_[0]->{init_arg} }
199 sub type_constraint { $_[0]->{type_constraint} }
201 sub trigger { $_[0]->{trigger} }
202 sub builder { $_[0]->{builder} }
203 sub should_auto_deref { $_[0]->{auto_deref} }
204 sub should_coerce { $_[0]->{coerce} }
208 sub has_accessor { exists $_[0]->{accessor} }
209 sub has_reader { exists $_[0]->{reader} }
210 sub has_writer { exists $_[0]->{writer} }
211 sub has_predicate { exists $_[0]->{predicate} }
212 sub has_clearer { exists $_[0]->{clearer} }
213 sub has_handles { exists $_[0]->{handles} }
215 sub has_default { exists $_[0]->{default} }
216 sub has_type_constraint { exists $_[0]->{type_constraint} }
217 sub has_trigger { exists $_[0]->{trigger} }
218 sub has_builder { exists $_[0]->{builder} }
221 $_[0]->{_create_args} = $_[1] if @_ > 1;
222 $_[0]->{_create_args}
225 sub accessor_metaclass { 'Mouse::Meta::Method::Accessor' }
228 my ($self, $class, $name, %args) = @_;
230 my $attribute = $self->new($name, %args);
232 $attribute->_create_args(\%args);
234 $class->add_attribute($attribute);
235 weaken($attribute->{associated_class} = $class);
237 my $associated_methods = 0;
239 my $generator_class = $self->accessor_metaclass;
240 foreach my $type(qw(accessor reader writer predicate clearer handles)){
241 if(exists $attribute->{$type}){
242 my $installer = '_install_' . $type;
243 $generator_class->$installer($attribute, $attribute->{$type}, $class);
244 $associated_methods++;
248 if($associated_methods == 0 && ($attribute->_is_metadata || '') ne 'bare'){
249 Carp::cluck(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
256 sub verify_against_type_constraint {
257 my ($self, $value) = @_;
258 my $tc = $self->type_constraint;
262 return 1 if $tc->check($value);
264 $self->verify_type_constraint_error($self->name, $value, $tc);
267 sub verify_type_constraint_error {
268 my($self, $name, $value, $type) = @_;
269 $self->throw_error("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
272 sub coerce_constraint { ## my($self, $value) = @_;
273 my $type = $_[0]->{type_constraint}
275 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
278 sub _canonicalize_handles {
282 if (ref($handles) eq 'HASH') {
285 elsif (ref($handles) eq 'ARRAY') {
286 return map { $_ => $_ } @$handles;
289 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
297 my %args = ($self->get_parent_args($class, $name), @_);
299 $self->create($class, $name, %args);
302 sub get_parent_args {
307 for my $super ($class->linearized_isa) {
308 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
310 return %{ $super_attr->_create_args };
313 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
319 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
320 $metaclass->throw_error(@_, depth => 1);
329 Mouse::Meta::Attribute - attribute metaclass
333 =head2 new %args -> Mouse::Meta::Attribute
335 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
337 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
339 Creates a new attribute in OwnerClass. Accessors and helper methods are
340 installed. Some error checking is done.
342 =head2 name -> AttributeName
344 =head2 associated_class -> OwnerClass
346 =head2 is_required -> Bool
348 =head2 default -> Item
350 =head2 has_default -> Bool
352 =head2 is_lazy -> Bool
354 =head2 predicate -> MethodName | Undef
356 =head2 has_predicate -> Bool
358 =head2 clearer -> MethodName | Undef
360 =head2 has_clearer -> Bool
362 =head2 handles -> { LocalName => RemoteName }
364 =head2 has_handles -> Bool
366 =head2 is_weak_ref -> Bool
368 =head2 init_arg -> Str
370 =head2 type_constraint -> Str
372 =head2 has_type_constraint -> Bool
374 =head2 trigger => CODE | Undef
376 =head2 has_trigger -> Bool
378 =head2 builder => MethodName | Undef
380 =head2 has_builder -> Bool
382 =head2 is_lazy_build => Bool
384 =head2 should_auto_deref -> Bool
386 Informational methods.
388 =head2 verify_against_type_constraint Item -> 1 | ERROR
390 Checks that the given value passes this attribute's type constraint. Returns 1
391 on success, otherwise C<confess>es.
393 =head2 canonicalize_args Name, %args -> %args
395 Canonicalizes some arguments to create. In particular, C<lazy_build> is
396 canonicalized into C<lazy>, C<builder>, etc.
398 =head2 validate_args Name, \%args -> 1 | ERROR
400 Checks that the arguments to create the attribute (ie those specified by
403 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
405 Creates a new attribute in OwnerClass, inheriting options from parent classes.
406 Accessors and helper methods are installed. Some error checking is done.
408 =head2 get_parent_args OwnerClass, AttributeName -> Hash
410 Returns the options that the parent class of C<OwnerClass> used for attribute