Skip to content

Commit

Permalink
ModulesPerl6::Model::SiteTips is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Nov 21, 2015
1 parent 4639c48 commit 70c9ef5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
1 change: 1 addition & 0 deletions Build.PL
Expand Up @@ -45,6 +45,7 @@ my $build = Module::Build->new(
'Mojolicious::Commands' => 0,
'Mojolicious::Plugin::AssetPack' => '0.68',
'Mojolicious::Plugin::Bootstrap3' => '3.3505',
'Moo' => '2.000002',
'POSIX' => 0,
'Test::Mojo::Role::Debug' => '1.003003',
'Test::Mojo::Role::ElementCounter' => '1.001005',
Expand Down
104 changes: 104 additions & 0 deletions web/lib/ModulesPerl6/Model/SiteTips.pm
@@ -0,0 +1,104 @@
package ModulesPerl6::Model::SiteTips;

use strictures 2;
use Carp qw/croak/;
use File::Spec::Functions qw/catfile/;
use FindBin; FindBin->again;
use Mojo::Util qw/trim/;

use Moo;
use namespace::clean;

has _tips => ( is => 'rw' );

has _tip_file => (
is => 'ro',
init_arg => 'tip_file',
default => sub {
$ENV{MODULESPERL6_TIP_FILE}
// catfile $FindBin::Bin, qw/.. site-tips.txt/;
},
trigger => 1,
);

sub _trigger__tip_file {
my ( $self, $file ) = @_;

open my $fh, '<', $file
or croak "Could not open site tips file for reading: $!";

my @tips;
while ( <$fh> ) {
$_ = trim $_;
next unless /\S/ and not /^#/;
push @tips, $_;
}

$self->_tips( \@tips );
}

sub tip {
my $self = shift;
my $tips = $self->_tips;
return $tips->[ rand @$tips ];
}

1;

__END__
=encoding utf8
=head1 NAME
ModulesPerl6::Model::SiteTips - model representing site usage tips for users
=head1 SYNOPSIS
my $m = ModulesPerl6::Model::SiteTips->new( tip_file => 'tips.txt' );
say $m->tip;
=head1 DESCRIPTION
This module is used to access site usage tips that are shown to users.
=head1 TIP FILE FORMAT
# This is a comment and should be ignored, as are blank lines
Tip 1
Tip 2
The tip file is just a regular text file where each tip occupies a single
line. Blank lines are ignored as are lines that start with C<#>
See L</tip_file> for info on where the module will look for the tip file.
=head1 METHODS
=head2 C<new>
my $m = ModulesPerl6::Model::SiteTips->new;
my $m = ModulesPerl6::Model::SiteTips->new( tip_file => 'tips.txt' );
Creates and returns a new C<ModulesPerl6::Model::SiteTips> object. Takes
these arguments:
=head2 C<tip_file>
my $m = ModulesPerl6::Model::SiteTips->new( tip_file => 'tips.txt' );
Specifies the file to use to read tips from. B<Defaults to:>
C<MODULESPERL6_TIP_FILE> environmental variable, if set, or
C<../site-tips.txt> relative to the location of the script.
=head1 CONTACT INFORMATION
Original version of this module was written by Zoffix Znet
(L<https://github.com/zoffixznet/>, C<Zoffix> on irc.freenode.net).
=head1 LICENSE
You can use and distribute this module under the same terms as Perl itself.
29 changes: 21 additions & 8 deletions web/t/01-models/03-site-tips.t
Expand Up @@ -15,13 +15,26 @@ use constant TEST_TIP_FILE => 't/01-models/03-site-tips-TEST-TIPS.txt';
use_ok MODEL;
my $m = MODEL->new( tip_file => TEST_TIP_FILE );
isa_ok $m => MODEL;
can_ok $m => qw/tip tip_file/;

is $m->tip_file, TEST_TIP_FILE, '->tip_file gives correct results';

my $is_wrong = 0;
diag 'Fetching tips many times...';
$m->tip =~ /^Tip \d\z/ or $is_wrong = 1 for 1 .. 2_000_000;
is $is_wrong, 0, '... all fetches were correct';
can_ok $m => qw/tip/;

subtest 'Fetching tips many times...' => sub {
my $is_wrong = 0;
my %seen_tips;
for ( 1 .. 2_000_000 ) {
my $tip = $m->tip;
$tip =~ /^Tip \d\z/ or $is_wrong = 1;
$seen_tips{ $tip } = 1;
}
is $is_wrong, 0, '... all fetches were correct';

if ( 2 > keys %seen_tips ) {
warn 'We got fewer than 2 tips. It might indicate a problem with code';
}
elsif ( 2 < keys %seen_tips ) {
fail 'More than 2 tips were seen. That must not happen'
}

diag "Seen these tips: " . join ', ', sort keys %seen_tips;
};

done_testing;

0 comments on commit 70c9ef5

Please sign in to comment.