stop closing over the method metaobject
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Class.pm
CommitLineData
3fef8ce8 1package Moose::Meta::TypeConstraint::Class;
2
3use strict;
4use warnings;
5use metaclass;
6
964294c1 7use B;
28412c0b 8use Scalar::Util 'blessed';
9use Moose::Util::TypeConstraints ();
3fef8ce8 10
28412c0b 11use base 'Moose::Meta::TypeConstraint';
3fef8ce8 12
336824fa 13__PACKAGE__->meta->add_attribute('class' => (
4078709c 14 reader => 'class',
336824fa 15));
16
964294c1 17my $inliner = sub {
18 my $self = shift;
19 my $val = shift;
20
3975b592 21 return 'Scalar::Util::blessed(' . $val . ')'
22 . ' && ' . $val . '->isa(' . B::perlstring($self->class) . ')';
964294c1 23};
24
3fef8ce8 25sub new {
336824fa 26 my ( $class, %args ) = @_;
27
964294c1 28 $args{parent}
29 = Moose::Util::TypeConstraints::find_type_constraint('Object');
30
31 my $class_name = $args{class};
32 $args{constraint} = sub { $_[0]->isa($class_name) };
33
34 $args{inlined} = $inliner;
35
36 my $self = $class->_new( \%args );
336824fa 37
dabed765 38 $self->compile_type_constraint();
336824fa 39
3fef8ce8 40 return $self;
41}
42
43sub parents {
44 my $self = shift;
45 return (
46 $self->parent,
336824fa 47 map {
48 # FIXME find_type_constraint might find a TC named after the class but that isn't really it
49 # I did this anyway since it's a convention that preceded TypeConstraint::Class, and it should DWIM
50 # if anybody thinks this problematic please discuss on IRC.
51 # a possible fix is to add by attr indexing to the type registry to find types of a certain property
52 # regardless of their name
d03bd989 53 Moose::Util::TypeConstraints::find_type_constraint($_)
54 ||
620db045 55 __PACKAGE__->new( class => $_, name => "__ANON__" )
1d9c3b75 56 } Class::MOP::class_of($self->class)->superclasses,
3fef8ce8 57 );
58}
59
d9e17f80 60sub equals {
61 my ( $self, $type_or_name ) = @_;
62
dabed765 63 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
d9e17f80 64
4c015454 65 return unless defined $other;
dabed765 66 return unless $other->isa(__PACKAGE__);
67
68 return $self->class eq $other->class;
d9e17f80 69}
70
3fef8ce8 71sub is_a_type_of {
d9e17f80 72 my ($self, $type_or_name) = @_;
3fef8ce8 73
d9e17f80 74 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
75
76 ($self->equals($type) || $self->is_subtype_of($type_or_name));
3fef8ce8 77}
78
79sub is_subtype_of {
d9e17f80 80 my ($self, $type_or_name_or_class ) = @_;
81
d9e17f80 82 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_class);
d03bd989 83
8ff79890 84 if ( not defined $type ) {
85 if ( not ref $type_or_name_or_class ) {
86 # it might be a class
87 return 1 if $self->class->isa( $type_or_name_or_class );
88 }
89 return;
90 }
d9e17f80 91
8ff79890 92 if ( $type->isa(__PACKAGE__) && $type->class ne $self->class) {
d9e17f80 93 # if $type_or_name_or_class isn't a class, it might be the TC name of another ::Class type
94 # or it could also just be a type object in this branch
95 return $self->class->isa( $type->class );
96 } else {
97 # the only other thing we are a subtype of is Object
98 $self->SUPER::is_subtype_of($type);
99 }
3fef8ce8 100}
101
97534a9b 102# This is a bit counter-intuitive, but a child type of a Class type
103# constraint is not itself a Class type constraint (it has no class
104# attribute). This whole create_child_type thing needs some changing
105# though, probably making MMC->new a factory or something.
106sub create_child_type {
2fb4885e 107 my ($self, @args) = @_;
108 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
97534a9b 109}
110
d414e17c 111sub get_message {
112 my $self = shift;
113 my ($value) = @_;
114
115 if ($self->has_message) {
116 return $self->SUPER::get_message(@_);
117 }
118
119 $value = (defined $value ? overload::StrVal($value) : 'undef');
8c063f8e 120 return "Validation failed for '" . $self->name . "' with value $value (not isa " . $self->class . ")";
d414e17c 121}
122
3fef8ce8 1231;
124
ad46f524 125# ABSTRACT: Class/TypeConstraint parallel hierarchy
126
3fef8ce8 127__END__
28412c0b 128
3fef8ce8 129=pod
130
9f4bf6b8 131=head1 DESCRIPTION
132
133This class represents type constraints for a class.
134
135=head1 INHERITANCE
136
137C<Moose::Meta::TypeConstraint::Class> is a subclass of
138L<Moose::Meta::TypeConstraint>.
139
3fef8ce8 140=head1 METHODS
141
142=over 4
143
9f4bf6b8 144=item B<< Moose::Meta::TypeConstraint::Class->new(%options) >>
145
146This creates a new class type constraint based on the given
147C<%options>.
148
149It takes the same options as its parent, with two exceptions. First,
150it requires an additional option, C<class>, which is name of the
151constraint's class. Second, it automatically sets the parent to the
152C<Object> type.
153
154The constructor also overrides the hand optimized type constraint with
155one it creates internally.
3fef8ce8 156
9f4bf6b8 157=item B<< $constraint->class >>
4078709c 158
9f4bf6b8 159Returns the class name associated with the constraint.
3fef8ce8 160
9f4bf6b8 161=item B<< $constraint->parents >>
3fef8ce8 162
9f4bf6b8 163Returns all the type's parent types, corresponding to its parent
164classes.
d9e17f80 165
2870fb09 166=item B<< $constraint->is_subtype_of($type_name_or_object) >>
3fef8ce8 167
9f4bf6b8 168If the given type is also a class type, then this checks that the
169type's class is a subclass of the other type's class.
3fef8ce8 170
38bf0e17 171Otherwise it falls back to the implementation in
9f4bf6b8 172L<Moose::Meta::TypeConstraint>.
3fef8ce8 173
9f4bf6b8 174=item B<< $constraint->create_child_type(%options) >>
3fef8ce8 175
9f4bf6b8 176This returns a new L<Moose::Meta::TypeConstraint> object with the type
177as its parent.
28412c0b 178
9f4bf6b8 179Note that it does I<not> return a
180C<Moose::Meta::TypeConstraint::Class> object!
97534a9b 181
f561b856 182=item B<< $constraint->get_message($value) >>
183
184This is the same as L<Moose::Meta::TypeConstraint/get_message> except
185that it explicitly says C<isa> was checked. This is to help users deal
186with accidentally autovivified type constraints.
187
3fef8ce8 188=back
189
28412c0b 190=head1 BUGS
191
d4048ef3 192See L<Moose/BUGS> for details on reporting bugs.
28412c0b 193
3fef8ce8 194=cut