lib/AI/MicroStructure/MultiList.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/AI/MicroStructure/List.pm ↑ Summary lib/AI/MicroStructure/linux.pm »
1 package AI::MicroStructure::MultiList;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AI::MicroStructure (); # do not export microname and friends
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use AI::MicroStructure::RemoteList;
5 use List::Util qw( shuffle );
6 use Carp;
7  
8 our @ISA = qw( AI::MicroStructure::RemoteList );
9  
10 sub init {
11 my $class = caller(0);
12 my $data = AI::MicroStructure->load_data($class);
13 no strict 'refs';
14  
15 my $sep = ${"$class\::Separator"} ||= '/';
16 my $tail = qr/$sep?[^$sep]*$/;
17  
18 # compute all categories
19 my @categories = ( [ $data->{names}, '' ] );
20 while ( my ( $h, $k ) = @{ shift @categories or []} ) {
21 if ( ref $h eq 'HASH' ) {
22 push @categories,
23 map { [ $h->{$_}, ( $k ? "$k$sep$_" : $_ ) ] } keys %$h;
24 }
25 else { # leaf
26 my @items = split /\s+/, $h;
27 while ($k) {
28 push @{ ${"$class\::MultiList"}{$k} }, @items;
29 $k =~ s!$tail!!;
30 }
31 }
32 }
33  
34 ${"$class\::Default"} = $data->{default} || ':all';
35 ${"$class\::structure"} = ( split /::/, $class )[-1];
36  
37 *{"$class\::import"} = sub {
38 my $callpkg = caller(0);
39 my $structure = ${"$class\::structure"};
40 my $micro = $class->new;
41 *{"$callpkg\::micro$structure"} = sub { $micro->name(@_) };
42 };
43  
44 ${"$class\::micro"} = $class->new();
45 }
46  
47 sub name {
48 my ( $self, $count ) = @_;
49 my $class = ref $self;
50  
51 if ( !$class ) { # called as a class method!
52 $class = $self;
53 no strict 'refs';
54 $self = ${"$class\::micro"};
55 }
56 no strict 'refs';
57  
58 if ( defined $count && $count !~ /A-Za-z/){
59 if($count eq 0 ) {
60 return wantarray
61 ? shuffle @{ $self->{base} }
62 : scalar @{ $self->{base} };
63 }
64 }
65  
66 $count ||= 1;
67 my $list = $self->{cache};
68 if ( @{ $self->{base} } ) {
69 push @$list, shuffle @{ $self->{base} } while @$list < $count;
70 }
71 splice( @$list, 0, $count );
72 }
73  
74 sub new {
75 my $class = shift;
76  
77 no strict 'refs';
78 my $self = bless { @_, cache => [] }, $class;
79  
80 # compute some defaults
81 $self->{category} ||= ${"$class\::Default"};
82  
83 # fall back to last resort (FIXME should we carp()?)
84 $self->{category} = ${"$class\::Default"}
85 if $self->{category} ne ':all'
86 && !exists ${"$class\::MultiList"}{ $self->{category} };
87  
88 $self->_compute_base();
89 return $self;
90 }
91  
92 sub _compute_base {
93 my ($self) = @_;
94 my $class = ref $self;
95  
96 # compute the base list for this category
97 no strict 'refs';
98 my %seen;
99 $self->{base} = [
100 grep { !$seen{$_}++ }
101 map { @{ ${"$class\::MultiList"}{$_} } }
102 $self->{category} eq ':all'
103 ? ( keys %{"$class\::MultiList"} )
104 : ( $self->{category} )
105 ];
106 return;
107 }
108  
109 sub category { $_[0]->{category} }
110  
111 sub categories {
112 my $class = shift;
113 $class = ref $class if ref $class;
114  
115 no strict 'refs';
116 return keys %{"$class\::MultiList"};
117 }
118  
119 sub has_category {
120 my ($class, $category) = @_;
121 $class = ref $class if ref $class;
122  
123 no strict 'refs';
124 return exists ${"$class\::MultiList"}{$category};
125 }
126  
127 sub structure {
128 my $class = ref $_[0] || $_[0];
129 no strict 'refs';
130 return ${"$class\::structure"};
131 }
132  
133  
134  
135 1;
136  
137 __END__
138  
« lib/AI/MicroStructure/List.pm ↑ Summary lib/AI/MicroStructure/linux.pm »