lib/Video/PlaybackMachine/FillProducer/Chooser.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 109 CC 4 SCAR 13.9
CC
4
Cov
0%
_reload_items
6.0
_get_new_items
6.0
_random_sort
6.0
CRAP 4.0
1 package Video::PlaybackMachine::FillProducer::Chooser;
2  
3 our $VERSION = '0.09'; # VERSION
4  
5 ####
6 #### Video::PlaybackMachine::FillProducer::Chooser
7 ####
8 #### $Revision$
9 ####
10 #### Picks content in random or quasi-random order. Does not pick
11 #### anything twice until all have been chosen. Will preferentially display
12 #### new content.
13 ####
14  
15 use Moo;
16  
17 with 'Video::PlaybackMachine::Logger';
18  
19 use IO::Dir;
20 use File::stat;
21  
22 ############################# Attributes ###########################
23  
24 has 'DIRECTORY' => (
25 is => 'ro',
26 required => 1
27 );
28  
29 has 'FILTER' => (
30 is => 'ro'
31 );
32  
33 has 'SEEN' => (
34 is => 'rw',
35 default => sub { return {} }
36 );
37  
38 has 'ITEMS' => (
39 is => 'rw',
40 default => sub { return []; }
41 );
42  
43 ############################## Class Methods ############################
44  
45 ##
46 ## new()
47 ##
48 ## Arguments: (hash)
49 ## DIRECTORY: string -- Directory from which we should choose things
50 ## FILTER: regexp -- Regular expression matching things we should return
51 ##
52  
53 ############################# Object Methods ##############################
54  
55 ##
56 ## choose()
57 ##
58 ## Returns an item from a randomly-selected list. Items which have
59 ## appeared during program run are played sooner.
60 ##
61 sub choose
62 {
63 my $self = shift;
64  
65 $self->_reload_items();
66  
67 return shift @{ $self->ITEMS() };
68  
69 }
70  
71 ##
72 ## is_available()
73 ##
74 ## Returns true if calling choose() would return an item.
75 ##
76 sub is_available
77 {
78 my $self = shift;
79  
80 $self->_reload_items();
81 return @{ $self->ITEMS() } > 0;
82 }
83  
84 ##
85 ## Loads any new items (i.e. files)
86 ##
87 sub _reload_items
88 {
89 my $self = shift;
90 my @new_items = $self->_get_new_items();
91 $self->ITEMS( [ @new_items, @{ $self->{'ITEMS'} } ] );
92  
93 if ( @{ $self->ITEMS() } == 0 )
94 {
95 $self->SEEN({});
96 $self->ITEMS( [ $self->_get_new_items() ] );
97 }
98  
99 return 1;
100 }
101  
102 sub _get_new_items
103 {
104 my $self = shift;
105  
106 -d $self->DIRECTORY()
107 or do
108 {
109 $self->warn("Directory '", $self->DIRECTORY(), "' does not exist");
110 return;
111 };
112 my $dh = IO::Dir->new( $self->DIRECTORY() )
113 or die "Couldn't open directory $self->DIRECTORY(): $!; stopped";
114 my @files = ();
115 while ( my $file = $dh->read() )
116 {
117 $file =~ /\.^/ and next;
118 if ( $self->FILTER )
119 {
120 my $filter = $self->FILTER();
121 $file =~ /$filter/ or next;
122 }
123 my $full_file = $self->DIRECTORY() . "/$file";
124 -f $full_file or next;
125 $self->SEEN()->{$full_file}++ and next;
126 push( @files, $full_file );
127 }
128  
129 return $self->_random_sort(@files);
130 }
131  
132 sub _random_sort
133 {
134 my $self = shift;
135 my (@items) = @_;
136  
137 my %st = ();
138 my $getval = sub {
139 my ($x) = @_;
140  
141 if ( !exists $st{$x} )
142 {
143 $st{$x} = rand();
144 }
145 return $st{$x};
146 };
147 return sort { $getval->($a) <=> $getval->($b) } @items;
148  
149 }
150  
151 no Moo;
152  
153 1;