| 1 | package Test::Run::Class::Hierarchy; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | =head1 NAME | ||||||||||||||
| 7 | |||||||||||||||
| 8 | Test::Run::Class::Hierarchy - returns a list of super-classes in topological | ||||||||||||||
| 9 | order. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 SYNPOSIS | ||||||||||||||
| 12 | |||||||||||||||
| 13 | use Test::Run::Class::Hierarchy; | ||||||||||||||
| 14 | |||||||||||||||
| 15 | my $base_classes = hierarchy_of("MyClass::Sub::Sub::Sub"); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | my $base_classes_rev = rev_hierarchy_of("MyClass::Sub::Sub::Sub"); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Returns a list of classes in the current namespace. Note that it caches | ||||||||||||||
| 22 | the results. | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 EXPORTS | ||||||||||||||
| 25 | |||||||||||||||
| 26 | =cut | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 25 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | 25 | extends('Exporter'); | |||||||||||||
| 31 | |||||||||||||||
| 32 | 25 | ▶ | use List::MoreUtils (qw(uniq)); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 25 | our @EXPORT_OK = (qw(hierarchy_of rev_hierarchy_of)); | |||||||||||||
| 35 | |||||||||||||||
| 36 | 25 | our %_hierarchy_of = (); | |||||||||||||
| 37 | |||||||||||||||
| 38 | =head2 my [@list] = hierarchy_of($class) | ||||||||||||||
| 39 | |||||||||||||||
| 40 | Returns a list of the classes in the hierarchy of the class, from bottom to | ||||||||||||||
| 41 | top. | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =cut | ||||||||||||||
| 44 | |||||||||||||||
| 45 | sub hierarchy_of | ||||||||||||||
| 46 | 4 | ▶ | { | ||||||||||||
|
Subroutine
hierarchy_of: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 47 | 4 | my $class = shift; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 4 | ▶ | if (exists($_hierarchy_of{$class})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | { | ||||||||||||||
| 51 | 2 | return $_hierarchy_of{$class}; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 25 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 2 | my @hierarchy = $class; | |||||||||||||
| 57 | 2 | my @parents = @{$class. '::ISA'}; | |||||||||||||
| 58 | |||||||||||||||
| 59 | 2 | ▶ | while (my $p = shift(@parents)) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | { | ||||||||||||||
| 61 | 8 | push @hierarchy, $p; | |||||||||||||
| 62 | 8 | push @parents, @{$p. '::ISA'}; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 2 | my @unique = uniq(@hierarchy); | |||||||||||||
| 66 | |||||||||||||||
| 67 | return $_hierarchy_of{$class} = | ||||||||||||||
| 68 | [ | ||||||||||||||
| 69 | sort | ||||||||||||||
| 70 | { | ||||||||||||||
| 71 | 2 | ▶ | $a->isa($b) ? -1 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | : $b->isa($a) ? +1 | ||||||||||||||
| 73 | : 0 | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | @unique | ||||||||||||||
| 76 | ]; | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 25 | our %_rev_hierarchy_of = (); | |||||||||||||
| 80 | |||||||||||||||
| 81 | =head2 my [@list] = rev_hierarchy_of($class) | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Returns the classes from top to bottom. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =cut | ||||||||||||||
| 86 | |||||||||||||||
| 87 | sub rev_hierarchy_of | ||||||||||||||
| 88 | 4 | ▶ | { | ||||||||||||
|
Subroutine
rev_hierarchy_of: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 89 | 4 | my $class = shift; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 4 | ▶ | if (exists($_rev_hierarchy_of{$class})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | { | ||||||||||||||
| 93 | 2 | return $_rev_hierarchy_of{$class}; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 2 | return $_rev_hierarchy_of{$class} = [reverse @{hierarchy_of($class)}]; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 25 | 1; | |||||||||||||
| 100 | |||||||||||||||
| 101 | =head1 LICENSE | ||||||||||||||
| 102 | |||||||||||||||
| 103 | This file is licensed under the MIT X11 License: | ||||||||||||||
| 104 | |||||||||||||||
| 105 | http://www.opensource.org/licenses/mit-license.php | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 CREDITS | ||||||||||||||
| 108 | |||||||||||||||
| 109 | The code was inspired by the code from Damian Conway's L<Class::Std>, but | ||||||||||||||
| 110 | is not inclusive of it. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Written by Shlomi Fish: L<http://www.shlomifish.org/>. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||
| 115 | |||||||||||||||
| 116 | L<Class::Std>, L<Test::Run> | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =cut | ||||||||||||||
| 119 | |||||||||||||||