Changelog
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Class.pm
CommitLineData
3fef8ce8 1package Moose::Meta::TypeConstraint::Class;
2
3use strict;
4use warnings;
5use metaclass;
6
28412c0b 7use Scalar::Util 'blessed';
8use Moose::Util::TypeConstraints ();
3fef8ce8 9
28412c0b 10our $VERSION = '0.01';
11our $AUTHORITY = 'cpan:STEVAN';
3fef8ce8 12
28412c0b 13use base 'Moose::Meta::TypeConstraint';
3fef8ce8 14
336824fa 15__PACKAGE__->meta->add_attribute('class' => (
16 reader => 'class',
17));
18
3fef8ce8 19sub new {
336824fa 20 my ( $class, %args ) = @_;
21
22 $args{class} = $args{name} unless exists $args{class};
23
24 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object');
25
26 my $self = $class->meta->new_object(%args);
27
3fef8ce8 28 $self->compile_type_constraint()
29 unless $self->_has_compiled_type_constraint;
336824fa 30
3fef8ce8 31 return $self;
32}
33
34sub parents {
35 my $self = shift;
36 return (
37 $self->parent,
336824fa 38 map {
39 # FIXME find_type_constraint might find a TC named after the class but that isn't really it
40 # I did this anyway since it's a convention that preceded TypeConstraint::Class, and it should DWIM
41 # if anybody thinks this problematic please discuss on IRC.
42 # a possible fix is to add by attr indexing to the type registry to find types of a certain property
43 # regardless of their name
44 Moose::Util::TypeConstraints::find_type_constraint($_) || __PACKAGE__->new( name => $_ )
45 } $self->class->meta->superclasses,
3fef8ce8 46 );
47}
48
49sub hand_optimized_type_constraint {
28412c0b 50 my $self = shift;
336824fa 51 my $class = $self->class;
fd542f49 52 sub { blessed( $_[0] ) && $_[0]->isa($class) }
3fef8ce8 53}
54
55sub has_hand_optimized_type_constraint { 1 }
56
d9e17f80 57sub equals {
58 my ( $self, $type_or_name ) = @_;
59
60 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
61
62 if ( $type->isa(__PACKAGE__) ) {
63 return $self->class eq $type->class;
64 } else {
65 $self->SUPER::equals($type);
66 }
67}
68
3fef8ce8 69sub is_a_type_of {
d9e17f80 70 my ($self, $type_or_name) = @_;
3fef8ce8 71
d9e17f80 72 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
73
74 ($self->equals($type) || $self->is_subtype_of($type_or_name));
3fef8ce8 75}
76
77sub is_subtype_of {
d9e17f80 78 my ($self, $type_or_name_or_class ) = @_;
79
80 if ( not ref $type_or_name_or_class ) {
81 # it might be a class
82 return 1 if $self->class->isa( $type_or_name_or_class );
83 }
84
85 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_class);
86
87 if ( $type->isa(__PACKAGE__) ) {
88 # if $type_or_name_or_class isn't a class, it might be the TC name of another ::Class type
89 # or it could also just be a type object in this branch
90 return $self->class->isa( $type->class );
91 } else {
92 # the only other thing we are a subtype of is Object
93 $self->SUPER::is_subtype_of($type);
94 }
3fef8ce8 95}
96
971;
98
99__END__
28412c0b 100
3fef8ce8 101=pod
102
103=head1 NAME
104
105Moose::Meta::TypeConstraint::Class - Class/TypeConstraint parallel hierarchy
106
107=head1 METHODS
108
109=over 4
110
28412c0b 111=item B<new>
3fef8ce8 112
28412c0b 113=item B<hand_optimized_type_constraint>
3fef8ce8 114
28412c0b 115=item B<has_hand_optimized_type_constraint>
3fef8ce8 116
d9e17f80 117=item B<equals>
118
28412c0b 119=item B<is_a_type_of>
3fef8ce8 120
28412c0b 121=item B<is_subtype_of>
3fef8ce8 122
28412c0b 123=item B<parents>
3fef8ce8 124
125Return all the parent types, corresponding to the parent classes.
126
28412c0b 127=item B<meta>
128
3fef8ce8 129=back
130
28412c0b 131=head1 BUGS
132
133All complex software has bugs lurking in it, and this module is no
134exception. If you find a bug please either email me, or add the bug
135to cpan-RT.
136
137=head1 AUTHOR
138
139Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
140
141=head1 COPYRIGHT AND LICENSE
142
143Copyright 2006-2008 by Infinity Interactive, Inc.
144
145L<http://www.iinteractive.com>
146
147This library is free software; you can redistribute it and/or modify
148it under the same terms as Perl itself.
149
3fef8ce8 150=cut