File Coverage

blib/lib/PRANG/Util.pm
Criterion Covered Total %
statement 31 32 96.8
branch 8 10 80.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 44 48 91.6


line stmt bran cond sub pod time code
1              
2             package PRANG::Util;
3             $PRANG::Util::VERSION = '0.21';
4 11     11   88 use strict;
  11         31  
  11         340  
5 11     11   65 use warnings;
  11         30  
  11         439  
6              
7 11         175 use Sub::Exporter -setup =>
8 11     11   80 { exports => [qw(types_of)] };
  11         23  
9              
10 11     11   11985 use Set::Object qw(set);
  11         88131  
  11         2720  
11              
12             # 12:20 <@mugwump> is there a 'Class::MOP::Class::subclasses' for roles?
13             # 12:20 <@mugwump> I want a list of classes that implement a role
14             # 12:37 <@autarch> mugwump: I'd kind of like to see that in core
15             sub types_of {
16 13     13 0 51 my @types = @_;
17              
18             # resolve type names to meta-objects;
19 13         44 for (@types) {
20 13 100       70 if ( !ref $_ ) {
21 6         60 $_ = $_->meta;
22             }
23             }
24 13         325 my $known = set(@types);
25 13         380 my @roles = grep { $_->isa("Moose::Meta::Role") } @types;
  13         88  
26              
27 13 50       54 if (@roles) {
28 13         82 $known->remove(@roles);
29 13         72 for my $mc (Class::MOP::get_all_metaclass_instances) {
30 1292 100       64744 next if !$mc->isa("Moose::Meta::Class");
31 286 50       142341 next if $known->includes($mc);
32 286 100       497 if ( grep { $mc->does_role($_->name) } @roles ) {
  286         1006  
33 27         5168 $known->insert($mc);
34             }
35             }
36             }
37 13         2039 for my $class ( $known->members ) {
38 27         178 my @subclasses = map { $_->meta } $class->subclasses;
  0         0  
39 27         366 $known->insert(@subclasses);
40             }
41 13         154 $known->members;
42             }
43              
44             1;
45              
46             # Copyright (C) 2009, 2010 NZ Registry Services
47             #
48             # This program is free software: you can redistribute it and/or modify
49             # it under the terms of the Artistic License 2.0 or later.
50             #
51             # This program is distributed in the hope that it will be useful,
52             # but WITHOUT ANY WARRANTY; without even the implied warranty of
53             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54             # Artistic License 2.0 for more details.
55             #
56             # You should have received a copy of the Artistic License the file
57             # COPYING.txt. If not, see
58             # <http://www.perlfoundation.org/artistic_license_2_0>