File Coverage

blib/lib/Moose/Meta/TypeConstraint/Class.pm
Criterion Covered Total %
statement 57 61 93.4
branch 17 22 77.2
condition 6 6 100.0
subroutine 14 15 93.3
pod 7 7 100.0
total 101 111 90.9


line stmt bran cond sub pod time code
1             package Moose::Meta::TypeConstraint::Class;
2             our $VERSION = '2.2205';
3              
4 390     390   2965 use strict;
  390         932  
  390         12377  
5 390     390   2215 use warnings;
  390         948  
  390         9709  
6 390     390   2148 use metaclass;
  390         928  
  390         2183  
7              
8 390     390   3109 use B;
  390         1119  
  390         19614  
9 390     390   2721 use Scalar::Util ();
  390         1065  
  390         8824  
10 390     390   2465 use Moose::Util::TypeConstraints ();
  390         1100  
  390         9907  
11              
12 390     390   2241 use parent 'Moose::Meta::TypeConstraint';
  390         1070  
  390         2787  
13              
14             __PACKAGE__->meta->add_attribute('class' => (
15             reader => 'class',
16             Class::MOP::_definition_context(),
17             ));
18              
19             my $inliner = sub {
20             my $self = shift;
21             my $val = shift;
22              
23             return 'Scalar::Util::blessed(' . $val . ')'
24             . ' && ' . $val . '->isa(' . B::perlstring($self->class) . ')';
25             };
26              
27             sub new {
28 1737     1737 1 7481 my ( $class, %args ) = @_;
29              
30             $args{parent}
31 1737         5817 = Moose::Util::TypeConstraints::find_type_constraint('Object');
32              
33 1737         4998 my $class_name = $args{class};
34 1737     16   10439 $args{constraint} = sub { $_[0]->isa($class_name) };
  16         101  
35              
36 1737         4940 $args{inlined} = $inliner;
37              
38 1737         11560 my $self = $class->SUPER::new( \%args );
39              
40 1737         7142 $self->compile_type_constraint();
41              
42 1737         10564 return $self;
43             }
44              
45             sub parents {
46 0     0 1 0 my $self = shift;
47             return (
48             $self->parent,
49             map {
50             # FIXME find_type_constraint might find a TC named after the class but that isn't really it
51             # I did this anyway since it's a convention that preceded TypeConstraint::Class, and it should DWIM
52             # if anybody thinks this problematic please discuss on IRC.
53             # a possible fix is to add by attr indexing to the type registry to find types of a certain property
54             # regardless of their name
55 0 0       0 Moose::Util::TypeConstraints::find_type_constraint($_)
  0         0  
56             ||
57             __PACKAGE__->new( class => $_, name => "__ANON__" )
58             } Class::MOP::class_of($self->class)->superclasses,
59             );
60             }
61              
62             sub equals {
63 11     11 1 27 my ( $self, $type_or_name ) = @_;
64              
65 11         32 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
66              
67 11 100       41 if (!defined($other)) {
68 4 50       12 if (!ref($type_or_name)) {
69 4         127 return $self->class eq $type_or_name;
70             }
71 0         0 return;
72             }
73              
74 7 50       28 return unless $other->isa(__PACKAGE__);
75              
76 7         252 return $self->class eq $other->class;
77             }
78              
79             sub is_a_type_of {
80 7     7 1 24 my ($self, $type_or_name) = @_;
81              
82 7 100       18 ($self->equals($type_or_name) || $self->is_subtype_of($type_or_name));
83             }
84              
85             sub is_subtype_of {
86 18     18 1 50 my ($self, $type_or_name_or_class ) = @_;
87              
88 18         52 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_class);
89              
90 18 100       52 if ( not defined $type ) {
91 6 50       20 if ( not ref $type_or_name_or_class ) {
92             # it might be a class
93 6         223 my $class = $self->class;
94 6 100 100     69 return 1 if $class ne $type_or_name_or_class
95             && $class->isa( $type_or_name_or_class );
96             }
97 4         22 return;
98             }
99              
100 12 100 100     394 if ( $type->isa(__PACKAGE__) && $type->class ne $self->class) {
101             # if $type_or_name_or_class isn't a class, it might be the TC name of another ::Class type
102             # or it could also just be a type object in this branch
103 5         139 return $self->class->isa( $type->class );
104             } else {
105             # the only other thing we are a subtype of is Object
106 7         41 $self->SUPER::is_subtype_of($type);
107             }
108             }
109              
110             # This is a bit counter-intuitive, but a child type of a Class type
111             # constraint is not itself a Class type constraint (it has no class
112             # attribute). This whole create_child_type thing needs some changing
113             # though, probably making MMC->new a factory or something.
114             sub create_child_type {
115 12     12 1 45 my ($self, @args) = @_;
116 12         50 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
117             }
118              
119             sub get_message {
120 35     35 1 75 my $self = shift;
121 35         76 my ($value) = @_;
122              
123 35 100       1115 if ($self->has_message) {
124 2         21 return $self->SUPER::get_message(@_);
125             }
126              
127 33 100       156 $value = (defined $value ? overload::StrVal($value) : 'undef');
128 33         1098 return "Validation failed for '" . $self->name . "' with value $value (not isa " . $self->class . ")";
129             }
130              
131             1;
132              
133             # ABSTRACT: Class/TypeConstraint parallel hierarchy
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             Moose::Meta::TypeConstraint::Class - Class/TypeConstraint parallel hierarchy
144              
145             =head1 VERSION
146              
147             version 2.2205
148              
149             =head1 DESCRIPTION
150              
151             This class represents type constraints for a class.
152              
153             =head1 INHERITANCE
154              
155             C<Moose::Meta::TypeConstraint::Class> is a subclass of
156             L<Moose::Meta::TypeConstraint>.
157              
158             =head1 METHODS
159              
160             =head2 Moose::Meta::TypeConstraint::Class->new(%options)
161              
162             This creates a new class type constraint based on the given
163             C<%options>.
164              
165             It takes the same options as its parent, with two exceptions. First,
166             it requires an additional option, C<class>, which is name of the
167             constraint's class. Second, it automatically sets the parent to the
168             C<Object> type.
169              
170             The constructor also overrides the hand optimized type constraint with
171             one it creates internally.
172              
173             =head2 $constraint->class
174              
175             Returns the class name associated with the constraint.
176              
177             =head2 $constraint->parents
178              
179             Returns all the type's parent types, corresponding to its parent
180             classes.
181              
182             =head2 $constraint->is_subtype_of($type_name_or_object)
183              
184             If the given type is also a class type, then this checks that the
185             type's class is a subclass of the other type's class.
186              
187             Otherwise it falls back to the implementation in
188             L<Moose::Meta::TypeConstraint>.
189              
190             =head2 $constraint->create_child_type(%options)
191              
192             This returns a new L<Moose::Meta::TypeConstraint> object with the type
193             as its parent.
194              
195             Note that it does I<not> return a
196             C<Moose::Meta::TypeConstraint::Class> object!
197              
198             =head2 $constraint->get_message($value)
199              
200             This is the same as L<Moose::Meta::TypeConstraint/get_message> except
201             that it explicitly says C<isa> was checked. This is to help users deal
202             with accidentally autovivified type constraints.
203              
204             =head1 BUGS
205              
206             See L<Moose/BUGS> for details on reporting bugs.
207              
208             =head1 AUTHORS
209              
210             =over 4
211              
212             =item *
213              
214             Stevan Little <stevan@cpan.org>
215              
216             =item *
217              
218             Dave Rolsky <autarch@urth.org>
219              
220             =item *
221              
222             Jesse Luehrs <doy@cpan.org>
223              
224             =item *
225              
226             Shawn M Moore <sartak@cpan.org>
227              
228             =item *
229              
230             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
231              
232             =item *
233              
234             Karen Etheridge <ether@cpan.org>
235              
236             =item *
237              
238             Florian Ragwitz <rafl@debian.org>
239              
240             =item *
241              
242             Hans Dieter Pearcey <hdp@cpan.org>
243              
244             =item *
245              
246             Chris Prather <chris@prather.org>
247              
248             =item *
249              
250             Matt S Trout <mstrout@cpan.org>
251              
252             =back
253              
254             =head1 COPYRIGHT AND LICENSE
255              
256             This software is copyright (c) 2006 by Infinity Interactive, Inc.
257              
258             This is free software; you can redistribute it and/or modify it under
259             the same terms as the Perl 5 programming language system itself.
260              
261             =cut