File Coverage

lib/Egg/Plugin/Banner/Rotate.pm
Criterion Covered Total %
statement 15 50 30.0
branch 0 16 0.0
condition 0 21 0.0
subroutine 5 12 41.6
pod 1 1 100.0
total 21 100 21.0


line stmt bran cond sub pod time code
1             package Egg::Plugin::Banner::Rotate;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Rotate.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   473 use strict;
  1         2  
  1         54  
8 1     1   5 use warnings;
  1         4  
  1         212  
9              
10             our $VERSION= '3.01';
11              
12             sub _setup {
13 0     0     my($e)= @_;
14 0   0       my $c= $e->config->{plugin_banner_rotate} ||= {};
15 0   0       $c->{base_dir} ||= $e->config->{dir}{etc}. '/banners';
16 0           $c->{base_dir}=~s{/+$} [];
17 0   0       $c->{extention} ||= 'yaml';
18 0           $c->{extention}=~s{^\.+} [];
19 0           $e->next::method;
20             }
21             sub banner_rotate {
22 0   0 0 1   $_[0]->{banner_rotate} ||= Egg::Plugin::Banner::Rotate::handler->new(@_);
23             }
24              
25             package Egg::Plugin::Banner::Rotate::handler;
26 1     1   6 use strict;
  1         4  
  1         33  
27 1     1   4 use base qw/ Egg::Base /;
  1         3  
  1         98  
28 1     1   6 use YAML;
  1         2  
  1         2449  
29              
30             sub new {
31 0     0     my($class, $e)= @_;
32 0           $class->SUPER::new($e, $e->config->{plugin_banner_rotate});
33             }
34             sub banners {
35 0     0     my $self = shift;
36 0   0       my $key = shift || 'default';
37 0   0       my $cache= $self->e->global->{banner_rotate} ||= {};
38 0   0       $cache->{$key} ||= do {
39 0           my $pm = $self->params;
40 0           my $yaml= "$pm->{base_dir}/$key.$pm->{extention}";
41 0           my $list= [ YAML::LoadFile($yaml) ];
42 0 0         @$list || warn qq{ Banners is not found - $yaml. };
43             {
44 0           num => 0,
45             time => time,
46             total => scalar(@$list),
47             banners => $list,
48             };
49             };
50             }
51             sub get_random {
52 0     0     rand(1000);
53 0           my $self= shift;
54 0           my $b= $self->banners(@_);
55 0 0         return {} unless $b->{total};
56 0 0         $b->{banners}[int( rand($b->{total}) )] || {};
57             }
58             sub get_turns {
59 0     0     my $self= shift;
60 0           my $b= $self->banners(@_);
61 0 0         return {} unless $b->{total};
62 0 0         $b->{num}= 0 if $b->{num}>= $b->{total};
63 0 0         $b->{banners}[$b->{num}++] || {};
64             }
65             sub clear_cache {
66 0     0     my $self= shift;
67 0   0       my $cache= $self->e->global->{banner_rotate} || return (undef);
68 0 0         if (@_) {
69 0   0       my $key = shift || return (undef);
70 0 0         $cache->{$key} ? delete($cache->{$key}): (undef);
71             } else {
72 0           %$cache= ();
73             }
74             }
75              
76             1;
77              
78             __END__
79              
80             =head1 NAME
81              
82             Egg::Plugin::Banner::Rotate - Plugin to display advertisement rotating.
83              
84             =head1 SYNOPSIS
85              
86             use Egg qw/ Banner::Rotate /;
87            
88             my $banner= $e->banner_rotate->get_random('banner_name');
89            
90             $e->stash->{head_banner}= qq{<a href="$banner->{url}"><img src="$banner->{img_url}" /></a>};
91              
92             =head1 DESCRIPTION
93              
94             The method that can be acquired by switching the data of the advertisement
95             registered beforehand in every case is offered.
96              
97             Please make a suitable data file for advertising data beforehand by the YAML format.
98             This advertising data is a thing composed of ARRAY without fail.
99              
100             ---
101             url: http://banner/redirect/hoo.html
102             img_url: http://banner/images/hoo.gif
103             ---
104             url: http://banner/redirect/hoge.html
105             img_url: http://banner/images/hoge.gif
106              
107             The element in ARRAY is not very cared about.
108             It only has to make data a convenient at the time of receipt format.
109              
110             =head1 CONFIGURATION
111              
112             The configuration is set with the key 'plugin_banner_rotate'.
113              
114             plugin_banner_rotate => {
115             ..........
116             ....
117             },
118              
119             =head2 base_dir
120              
121             It is passing of the directory that sets up advertising data.
122              
123             Default is "$e-E<gt>config-E<gt>{dir}{etc}/banners".
124              
125             base_dir => '<e.dir.etc>/banners',
126              
127             =head2 extention
128              
129             Extension of advertising data file.
130              
131             Default is 'yaml'.
132              
133             extention => 'yaml',
134              
135             =head1 METHODS
136              
137             =head2 banner_rotate
138              
139             The Egg::Plugin::Banner::Rotate::handler object is returned.
140              
141             my $br= $e->banner_rotate;
142              
143             =head1 HANDLER METHODS
144              
145             L<Egg::Base> has been succeeded to.
146              
147             =head2 new
148              
149             Constructor. When 'banner_rotate' method is called, it is called internally.
150              
151             =head2 banners ([BANNER_NAME])
152              
153             The registered advertising data is returned.
154              
155             BANNER_NAME specifies the part of the file name that doesn't contain the extension
156             of advertising data.
157              
158             When BANNER_NAME is omitted, 'default' is used.
159              
160             The data specified for BANNER_NAME should exist in
161             "[base_dir]/[BANNER_NAME].[extention]".
162              
163             If data file is not found, the exception is generated by 'LoadFile' of L<YAML>.
164              
165             The returned advertising data is HASH reference with the following keys.
166              
167             =over 4
168              
169             =item * banners = List data of advertisement (ARRAY_REF).
170              
171             =item * total = Registered advertising number.
172              
173             =item * time = time value when data is read.
174              
175             =item * num = Rotation number.
176              
177             =back
178              
179             my $hash= $br->banners('banner_name');
180            
181             for (@{$hash->{banners}}) {
182             .........
183             ....
184             }
185              
186             =head2 get_random ([BANNER_NAME])
187              
188             Advertising data is returned from data obtained by 'banners' method at random.
189             The argument is passed to 'banners' method.
190              
191             my $banner= $br->get_random('hoge');
192              
193             The element of ARRAY registered in YAML extends as it is in data.
194              
195             =head2 get_turns ([BANNER_NAME])
196              
197             Advertising data is sequentially returned from data obtained by 'banners' method.
198             The argument is passed to 'banners' method.
199              
200             my $banner= $br->get_turns('hoge');
201              
202             The element of ARRAY registered in YAML extends as it is in data.
203              
204             =head2 clear_cache ([BANNER_NAME])
205              
206             This method clears it though 'banners' method uses cash.
207              
208             When BANNER_NAME is specified, only the corresponding cash is cleared.
209             When BANNER_NAME is unspecification, all cash is cleared.
210              
211             $br->clear_cache('booo');
212              
213             =head1 SEE ALSO
214              
215             L<Egg::Release>,
216             L<Egg::Base>,
217             L<YAML>,
218              
219             =head1 AUTHOR
220              
221             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
226              
227             This library is free software; you can redistribute it and/or modify
228             it under the same terms as Perl itself, either Perl version 5.8.6 or,
229             at your option, any later version of Perl 5 you may have available.
230              
231             =cut
232