diff --git a/Build.PL b/Build.PL index 02e8ecd..1b7570a 100755 --- a/Build.PL +++ b/Build.PL @@ -21,6 +21,10 @@ my $build = Module::Build->new( 'XML::Twig' => 0, 'JSON' => 0, 'IP::Geolocation::MMDB' => 0, + 'Moo' => 0, + 'Moo::Role' => 0, + 'Module::Pluggable' => 0, + 'List::AllUtils' => 0, }, ); $build->create_build_script; diff --git a/lib/BurguillosInfo/Ad.pm b/lib/BurguillosInfo/Ad.pm new file mode 100644 index 0000000..d52a6c1 --- /dev/null +++ b/lib/BurguillosInfo/Ad.pm @@ -0,0 +1,21 @@ +package BurguillosInfo::Ad; + +use v5.36.0; + +use strict; +use warnings; + +use feature 'signatures'; + +use Moo::Role; + +sub order { + return 999; +} + +sub seconds { + return 15; +} + +requires 'id is_active img text'; +1; diff --git a/lib/BurguillosInfo/Ads.pm b/lib/BurguillosInfo/Ads.pm new file mode 100644 index 0000000..e03011b --- /dev/null +++ b/lib/BurguillosInfo/Ads.pm @@ -0,0 +1,88 @@ +package BurguillosInfo::Ads; + +use v5.36.0; + +use strict; +use warnings; + +use feature 'signatures'; + +use List::AllUtils qw/none/; + +use Moo; +use Module::Pluggable + search_path => ['BurguillosInfo::Ads'], + instantiate => 'instance', + on_require_error => sub ( $plugin, $error ) { + die $error; + }; + +{ + my @array_ads; + + sub _array ($self) { + if ( !scalar @array_ads ) { + $self->_populate_array; + } + return [@array_ads]; + } + + sub _populate_array ($self) { + @array_ads = $self->plugins(); + for my $ad (@array_ads) { + $self->_check_ad_valid($ad); + } + @array_ads = sort { $self->_order_two_ads( $a, $b ); } @array_ads; + } +} + +sub _order_two_ads ( $self, $a, $b ) { + my $by_order = $a->order <=> $b->order; + if ($by_order) { + return $by_order; + } + my $by_alpha = $a->id cmp $b->id; + return $by_alpha; +} + +sub get_next ( $self, $current_ad_number = undef ) { + my $array = $self->_array; + if ( !scalar @$array + || none { $_->is_active } @$array ) + { + return { continue => 0, current_ad_number => undef }; + } + if ( !defined $current_ad_number ) { + $current_ad_number = 0; + } + my $ad = $array->[$current_ad_number]; + if ( !$ad->is_active ) { + return $self->get_next( $self->_get_next_number($current_ad_number) ); + } + return { + ad => $ad, + continue => 1, + current_ad_number => $self->_get_next_number($current_ad_number), + }; +} + +sub _get_next_number ( $self, $current_ad_number = undef ) { + my $array = $self->_array; + if ( !scalar @$array ) { + return undef; + } + if ( !defined $current_ad_number ) { + return 0; + } + if ( ++$current_ad_number > ( scalar @$array ) - 1 ) { + return 0; + } + return $current_ad_number; +} + +sub _check_ad_valid ( $self, $ad ) { + if ( !$ad->does('BurguillosInfo::Ad') ) { + die "$ad does not implement BurguillosInfo::Ad."; + } +} +1;