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.2206';
3              
4 390     390   3015 use strict;
  390         949  
  390         12393  
5 390     390   2242 use warnings;
  390         953  
  390         9675  
6 390     390   2078 use metaclass;
  390         978  
  390         2229  
7              
8 390     390   3147 use B;
  390         1092  
  390         19897  
9 390     390   2715 use Scalar::Util ();
  390         1139  
  390         8607  
10 390     390   2586 use Moose::Util::TypeConstraints ();
  390         1051  
  390         9806  
11              
12 390     390   2268 use parent 'Moose::Meta::TypeConstraint';
  390         1046  
  390         2716  
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 7648 my ( $class, %args ) = @_;
29              
30             $args{parent}
31 1737         5757 = Moose::Util::TypeConstraints::find_type_constraint('Object');
32              
33 1737         5025 my $class_name = $args{class};
34 1737     16   10167 $args{constraint} = sub { $_[0]->isa($class_name) };
  16         111  
35              
36 1737         4947 $args{inlined} = $inliner;
37              
38 1737         11747 my $self = $class->SUPER::new( \%args );
39              
40 1737         7872 $self->compile_type_constraint();
41              
42 1737         10268 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 22 my ( $self, $type_or_name ) = @_;
64              
65 11         33 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
66              
67 11 100       33 if (!defined($other)) {
68 4 50       9 if (!ref($type_or_name)) {
69 4         116 return $self->class eq $type_or_name;
70             }
71 0         0 return;
72             }
73              
74 7 50       26 return unless $other->isa(__PACKAGE__);
75              
76 7         245 return $self->class eq $other->class;
77             }
78              
79             sub is_a_type_of {
80 7     7 1 30 my ($self, $type_or_name) = @_;
81              
82 7 100       20 ($self->equals($type_or_name) || $self->is_subtype_of($type_or_name));
83             }
84              
85             sub is_subtype_of {
86 18     18 1 43 my ($self, $type_or_name_or_class ) = @_;
87              
88 18         59 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_class);
89              
90 18 100       49 if ( not defined $type ) {
91 6 50       17 if ( not ref $type_or_name_or_class ) {
92             # it might be a class
93 6         176 my $class = $self->class;
94 6 100 100     60 return 1 if $class ne $type_or_name_or_class
95             && $class->isa( $type_or_name_or_class );
96             }
97 4         28 return;
98             }
99              
100 12 100 100     396 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         155 return $self->class->isa( $type->class );
104             } else {
105             # the only other thing we are a subtype of is Object
106 7         34 $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 60 my ($self, @args) = @_;
116 12         48 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
117             }
118              
119             sub get_message {
120 35     35 1 68 my $self = shift;
121 35         84 my ($value) = @_;
122              
123 35 100       1127 if ($self->has_message) {
124 2         37 return $self->SUPER::get_message(@_);
125             }
126              
127 33 100       147 $value = (defined $value ? overload::StrVal($value) : 'undef');
128 33         1029 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.2206
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