| 1 | package AI::MicroStructure::MultiList; | ||
| 2 | use strict; | ||
| 3 | use AI::MicroStructure (); # do not export metaname and friends | ||
| 4 | use AI::MicroStructure::RemoteList; | ||
| 5 | use List::Util qw( shuffle ); | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | our @ISA = qw( AI::MicroStructure::RemoteList ); | ||
| 9 | our $VERSION = '1.000'; | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | my ($self, $data) = @_; | ||
| 13 | my $class = caller(0); | ||
| 14 | |||
| 15 | $data ||= AI::MicroStructure->load_data($class); | ||
| 16 | no strict 'refs'; | ||
| 17 | |||
| 18 | # note: variables mentioned twice to avoid a warning | ||
| 19 | |||
| 20 | my $sep = ${"$class\::Separator"} = ${"$class\::Separator"} ||= '/'; | ||
| 21 | my $tail = qr/$sep?[^$sep]*$/; | ||
| 22 | |||
| 23 | # compute all categories | ||
| 24 | my @categories = ( [ $data->{names}, '' ] ); | ||
| 25 | while ( my ( $h, $k ) = @{ shift @categories or []} ) { | ||
| 26 | if ( ref $h eq 'HASH' ) { | ||
| 27 | push @categories, | ||
| 28 | map { [ $h->{$_}, ( $k ? "$k$sep$_" : $_ ) ] } keys %$h; | ||
| 29 | } | ||
| 30 | else { # leaf | ||
| 31 | my @items = split /\s+/, $h; | ||
| 32 | while ($k) { | ||
| 33 | push @{ ${"$class\::MultiList"}{$k} }, @items; | ||
| 34 | $k =~ s!$tail!!; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | ${"$class\::Default"} = ${"$class\::Default"} = $data->{default} || ':all'; | ||
| 40 | ${"$class\::Theme"} = ${"$class\::Theme"} = ( split /::/, $class )[-1]; | ||
| 41 | |||
| 42 | *{"$class\::import"} = sub { | ||
| 43 | my $callpkg = caller(0); | ||
| 44 | my $structure = ${"$class\::Theme"}; | ||
| 45 | my $meta = $class->new; | ||
| 46 | *{"$callpkg\::meta$structure"} = sub { $meta->name(@_) }; | ||
| 47 | }; | ||
| 48 | |||
| 49 | ${"$class\::meta"} = ${"$class\::meta"} = $class->new(); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub name { | ||
| 53 | my ( $self, $count ) = @_; | ||
| 54 | my $class = ref $self; | ||
| 55 | |||
| 56 | if ( !$class ) { # called as a class method! | ||
| 57 | $class = $self; | ||
| 58 | no strict 'refs'; | ||
| 59 | $self = ${"$class\::meta"}; | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( defined $count && $count == 0 ) { | ||
| 63 | no strict 'refs'; | ||
| 64 | return wantarray | ||
| 65 | ? shuffle @{ $self->{base} } | ||
| 66 | : scalar @{ $self->{base} }; | ||
| 67 | } | ||
| 68 | |||
| 69 | $count ||= 1; | ||
| 70 | my $list = $self->{cache}; | ||
| 71 | if ( @{ $self->{base} } ) { | ||
| 72 | push @$list, shuffle @{ $self->{base} } while @$list < $count; | ||
| 73 | } | ||
| 74 | splice( @$list, 0, $count ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my $class = shift; | ||
| 79 | |||
| 80 | no strict 'refs'; | ||
| 81 | my $self = bless { @_, cache => [] }, $class; | ||
| 82 | |||
| 83 | # compute some defaults | ||
| 84 | $self->{category} ||= ${"$class\::Default"}; | ||
| 85 | |||
| 86 | # fall back to last resort (FIXME should we carp()?) | ||
| 87 | $self->{category} = ${"$class\::Default"} | ||
| 88 | if $self->{category} ne ':all' | ||
| 89 | && !exists ${"$class\::MultiList"}{ $self->{category} }; | ||
| 90 | |||
| 91 | $self->_compute_base(); | ||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _compute_base { | ||
| 96 | my ($self) = @_; | ||
| 97 | my $class = ref $self; | ||
| 98 | |||
| 99 | # compute the base list for this category | ||
| 100 | no strict 'refs'; | ||
| 101 | my %seen; | ||
| 102 | $self->{base} = [ | ||
| 103 | grep { !$seen{$_}++ } | ||
| 104 | map { @{ ${"$class\::MultiList"}{$_} } } | ||
| 105 | $self->{category} eq ':all' | ||
| 106 | ? ( keys %{"$class\::MultiList"} ) | ||
| 107 | : ( $self->{category} ) | ||
| 108 | ]; | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub category { $_[0]->{category} } | ||
| 113 | |||
| 114 | sub categories { | ||
| 115 | my $class = shift; | ||
| 116 | $class = ref $class if ref $class; | ||
| 117 | |||
| 118 | no strict 'refs'; | ||
| 119 | return keys %{"$class\::MultiList"}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub has_category { | ||
| 123 | my ($class, $category) = @_; | ||
| 124 | $class = ref $class if ref $class; | ||
| 125 | |||
| 126 | no strict 'refs'; | ||
| 127 | return exists ${"$class\::MultiList"}{$category}; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub structure { | ||
| 131 | my $class = ref $_[0] || $_[0]; | ||
| 132 | no strict 'refs'; | ||
| 133 | return ${"$class\::Theme"}; | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 | |||
| 138 | __END__ | ||
| 139 | |||
| 140 | =head1 NAME | ||
| 141 | |||
| 142 | AI::MicroStructure::MultiList - Base class for structures with multiple lists | ||
| 143 | |||
| 144 | =head1 SYNOPSIS | ||
| 145 | |||
| 146 | package AI::MicroStructure::digits; | ||
| 147 | use AI::MicroStructure::MultiList; | ||
| 148 | our @ISA = ( AI::MicroStructure::MultiList ); | ||
| 149 | __PACKAGE__->init(); | ||
| 150 | 1; | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | AI::MicroStructure::digits - The numbers structure | ||
| 155 | |||
| 156 | =head1 DESCRIPTION | ||
| 157 | |||
| 158 | You can count on this module. Almost. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | __DATA__ | ||
| 163 | # default | ||
| 164 | :all | ||
| 165 | # names primes even | ||
| 166 | two | ||
| 167 | # names primes odd | ||
| 168 | three five seven | ||
| 169 | # names composites even | ||
| 170 | four six eight | ||
| 171 | # names composites odd | ||
| 172 | nine | ||
| 173 | # names other | ||
| 174 | zero one | ||
| 175 | |||
| 176 | =head1 DESCRIPTION | ||
| 177 | |||
| 178 | C<AI::MicroStructure::MultiList> is the base class for all structures | ||
| 179 | that are meant to return a random excerpt from a predefined list | ||
| 180 | I<divided in categories>. | ||
| 181 | |||
| 182 | The category is selected at construction time from: | ||
| 183 | |||
| 184 | =over 4 | ||
| 185 | |||
| 186 | =item 1. | ||
| 187 | |||
| 188 | the given C<category> parameter, | ||
| 189 | |||
| 190 | =item 2. | ||
| 191 | |||
| 192 | the default category for the selected structure. | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | Categories and sub-categories are separated by a C</> character. | ||
| 197 | |||
| 198 | =head1 METHODS | ||
| 199 | |||
| 200 | AI::MicroStructure::MultiList offers several methods, so that the subclasses | ||
| 201 | are easy to write (see full example in L<SYNOPSIS>): | ||
| 202 | |||
| 203 | =over 4 | ||
| 204 | |||
| 205 | =item new( category => $category ) | ||
| 206 | |||
| 207 | The constructor of a single instance. An instance will not repeat items | ||
| 208 | until the list is exhausted. | ||
| 209 | |||
| 210 | $meta = AI::MicroStructure::digits->new( category => 'primes' ); | ||
| 211 | $meta = AI::MicroStructure::digits->new( category => 'primes/odd' ); | ||
| 212 | |||
| 213 | The special category C<:all> will use all the items in all categories. | ||
| 214 | |||
| 215 | $meta = AI::MicroStructure::digits->new( category => ':all' ); | ||
| 216 | |||
| 217 | If no C<category> parameter is given, C<AI::MicroStructure::MultiList> | ||
| 218 | will use the class default. If the class doesn't define a default, | ||
| 219 | then C<:all> is used. | ||
| 220 | |||
| 221 | =item init() | ||
| 222 | |||
| 223 | init() must be called when the subclass is loaded, so as to read the | ||
| 224 | __DATA__ section and fully initialise it. | ||
| 225 | |||
| 226 | =item name( $count ) | ||
| 227 | |||
| 228 | Return $count names (default: C<1>). | ||
| 229 | |||
| 230 | Using C<0> will return the whole list in list context, and the size of the | ||
| 231 | list in scalar context (according to the C<category> parameter passed to the | ||
| 232 | constructor). | ||
| 233 | |||
| 234 | =item category() | ||
| 235 | |||
| 236 | Return the selected category for this instance. | ||
| 237 | |||
| 238 | =item categories() | ||
| 239 | |||
| 240 | Return the categories supported by the structure (except C<:all>). | ||
| 241 | |||
| 242 | =item has_category( $category ) | ||
| 243 | |||
| 244 | Return a boolean value indicating if the structure contains the given category. | ||
| 245 | |||
| 246 | =item structure() | ||
| 247 | |||
| 248 | Return the structure name. | ||
| 249 | |||
| 250 | =back | ||
| 251 | |||
| 252 | =head1 AUTHOR | ||
| 253 | |||
| 254 | Philippe 'BooK' Bruhat, C<< <book@cpan.org> >> | ||
| 255 | |||
| 256 | =head1 COPYRIGHT & LICENSE | ||
| 257 | |||
| 258 | Copyright 2006 Philippe 'BooK' Bruhat, All Rights Reserved. | ||
| 259 | |||
| 260 | This program is free software; you can redistribute it and/or modify it | ||
| 261 | under the same terms as Perl itself. | ||
| 262 | |||
| 263 | =cut |