From 1e4ff30ff0e4e557937f5c47277da4e111306682 Mon Sep 17 00:00:00 2001 From: swag Date: Thu, 3 Nov 2022 14:03:48 -0400 Subject: [PATCH] Use subroutine for attribute rather than blessed hash reference value --- README.md | 1 - lib/PostText/Model/Remark.pm | 10 ++++++---- lib/PostText/Model/Thread.pm | 12 +++++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 171f707..920fd32 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ Run the tests locally (against development environment): ## TODOs -1. Fix the `x_per_page` attributes (should use `has`) 1. s/Authen::Passphrase::BlowfishCrypt/Crypt::Passphrase/g 1. Need to pass `config` to the Moderator model for bcrypt cost 1. Re-write bcrypt command to use Authen::Passphrase::BlowfishCrypt diff --git a/lib/PostText/Model/Remark.pm b/lib/PostText/Model/Remark.pm index 6c04d8d..a8fbddf 100644 --- a/lib/PostText/Model/Remark.pm +++ b/lib/PostText/Model/Remark.pm @@ -4,9 +4,11 @@ use Mojo::Base -base, -signatures; has 'pg'; +has per_page => sub { 5 }; + sub by_page_for($self, $thread_id, $this_page = 1) { my $date_format = $self->{'date_format'}; - my $row_count = $self->{'remarks_per_page'}; + my $row_count = $self->per_page; my $offset = ($this_page - 1) * $row_count; my @data = ($date_format, $thread_id, $row_count, $offset); @@ -24,7 +26,7 @@ sub by_page_for($self, $thread_id, $this_page = 1) { } sub per_page($self, $value = undef) { - $self->{'remarks_per_page'} = $value // $self->{'remarks_per_page'} + $self->per_page = $value // $self->per_page } sub create($self, $thread_id, $author, $body, $hidden = 0, $flagged = 0) { @@ -53,10 +55,10 @@ sub count_for($self, $thread_id) { sub last_page_for($self, $thread_id) { my $remark_count = $self->count_for($thread_id); - my $last_page = int($remark_count / $self->{'remarks_per_page'}); + my $last_page = int($remark_count / $self->per_page); # Add a page for 'remainder' posts - $last_page++ if $remark_count % $self->{'remarks_per_page'}; + $last_page++ if $remark_count % $self->per_page; return $last_page; } diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index 1e981af..3d83a93 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -4,6 +4,8 @@ use Mojo::Base -base, -signatures; has 'pg'; +has per_page => sub { 5 }; + sub create($self, $author, $title, $body, $hidden = 0, $flagged = 0) { my @data = ($author, $title, $body, $hidden, $flagged); @@ -22,7 +24,7 @@ sub create($self, $author, $title, $body, $hidden = 0, $flagged = 0) { sub by_page($self, $this_page = 1) { my $date_format = $self->{'date_format'}; - my $row_count = $self->{'threads_per_page'}; + my $row_count = $self->per_page; my $offset = ($this_page - 1) * $row_count; $self->pg->db @@ -44,16 +46,12 @@ sub by_page($self, $this_page = 1) { END_SQL } -sub per_page($self, $value = undef) { - $self->{'threads_per_page'} = $value // $self->{'threads_per_page'} -} - sub last_page($self) { my $thread_count = $self->count; - my $last_page = int($thread_count / $self->{'threads_per_page'}); + my $last_page = int($thread_count / $self->per_page); # Add a page for 'remainder' posts - $last_page++ if $thread_count % $self->{'threads_per_page'}; + $last_page++ if $thread_count % $self->per_page; $last_page; }