1 package IronMunger::Calculate;
6 use autobox::DateTime::Duration;
9 use Sub::Exporter -setup => {
11 qw(successful_sequential_posts time_remaining_to_post)
15 sub check_post_gap ($aperture, $days, @posts) {
16 return @posts if @posts <= $aperture;
17 my @next_post = splice(@posts, 0, $aperture);
18 return 0 if DateTime->now - $next_post[-1]->at > $days->days;
19 my $success = $aperture;
20 foreach my $post (@posts) {
21 return $success if $next_post[0]->at - $post->at > $days->days;
24 push(@next_post, $post);
29 sub check_time_remaining ($aperture, $days, @posts) {
30 my $cand = (@posts > $aperture ? $posts[$aperture - 1] : $posts[-1]);
31 return $days->days - (DateTime->now - $cand->at)->in_units('days');
34 sub check_both ($check, @posts) {
36 $check->(1, 10, @posts), # 10 days between posts
37 $check->(4, 32, @posts), # 4 posts within any given 32 days
41 sub successful_sequential_posts (@posts) {
42 return check_both(\&check_post_gap, @posts);
45 sub days_remaining_to_post (@posts) {
46 return check_both(\&check_time_remaining, @posts);