7df0e78d2a61b123cea30d0d4e7ea1ce2d702e5b
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Class.pm
1 package Moose::Meta::TypeConstraint::Class;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util 'blessed';
8 use Moose::Util::TypeConstraints ();
9
10 our $VERSION   = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::TypeConstraint';
14
15 sub new {
16     my $class = shift;
17     my $self  = $class->meta->new_object(@_, 
18         parent => Moose::Util::TypeConstraints::find_type_constraint('Object') 
19     );
20     $self->compile_type_constraint()
21         unless $self->_has_compiled_type_constraint;
22     return $self;
23 }
24
25 sub parents {
26     my $self = shift;
27     return (
28         $self->parent,
29         map { 
30             # NOTE:
31             # Hmm, should this be find_or_create_type_constraint?
32             # What do you think nothingmuch??
33             # - SL
34             Moose::Util::TypeConstraints::find_type_constraint($_) 
35         } $self->name->meta->superclasses,
36     );
37 }
38
39 sub hand_optimized_type_constraint {
40     my $self  = shift;
41     my $class = $self->name;
42     sub {
43       Moose::Util::TypeConstraints::OptimizedConstraints::ObjectOfType($_[0], $class)
44     }
45 }
46
47 sub has_hand_optimized_type_constraint { 1 }
48
49 sub is_a_type_of {
50     my ($self, $type_name) = @_;
51
52     return $self->name eq $type_name || $self->is_subtype_of($type_name);
53 }
54
55 sub is_subtype_of {
56     my ($self, $type_name) = @_;
57
58     return 1 if $type_name eq 'Object';
59     return $self->name->isa( $type_name );
60 }
61
62 1;
63
64 __END__
65
66 =pod
67
68 =head1 NAME
69
70 Moose::Meta::TypeConstraint::Class - Class/TypeConstraint parallel hierarchy
71
72 =head1 METHODS
73
74 =over 4
75
76 =item B<new>
77
78 =item B<hand_optimized_type_constraint>
79
80 =item B<has_hand_optimized_type_constraint>
81
82 =item B<is_a_type_of>
83
84 =item B<is_subtype_of>
85
86 =item B<parents>
87
88 Return all the parent types, corresponding to the parent classes.
89
90 =item B<meta>
91
92 =back
93
94 =head1 BUGS
95
96 All complex software has bugs lurking in it, and this module is no 
97 exception. If you find a bug please either email me, or add the bug
98 to cpan-RT.
99
100 =head1 AUTHOR
101
102 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
103
104 =head1 COPYRIGHT AND LICENSE
105
106 Copyright 2006-2008 by Infinity Interactive, Inc.
107
108 L<http://www.iinteractive.com>
109
110 This library is free software; you can redistribute it and/or modify
111 it under the same terms as Perl itself.
112
113 =cut