| 1 | package WebService::HabitRPG::Task; | ||||||||||||||||||||
| 2 | 5 | ▶ | use v5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | ▶ | use autodie; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 5 | ▶ | use JSON::MaybeXS qw(is_bool); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 5 | ▶ | use DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 5 | ▶ | use constant HRPG_REPEAT_MAP => qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | m t w th f s su | ||||||||||||||||||||
| 17 | 5 | ); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # TODO: croak provides poor error messages in here, possibly due to | ||||||||||||||||||||
| 20 | # it not knowing about Moo properly. Still, they're good enough for | ||||||||||||||||||||
| 21 | # getting stack backtraces when needed. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # ABSTRACT: A HabitRPG task | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | our $VERSION = '0.30'; # VERSION: Generated by DZP::OurPkg:Version | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Validation functions | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 9 | ▶ | my $Bool = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||
| 31 | 9 | my ( $value ) = @_; | |||||||||||||||||||
| 32 | 9 | ▶ | $value = 0 + $value if is_bool($value); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 9 | ▶ | croak "$value must be 0|1" unless $value =~ /^[01]$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 5 | }; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | my $Num = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 37 | 2 | ▶ | croak "$_[0] isn't a number" unless looks_like_number $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 5 | }; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 3 | ▶ | my $Type = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 41 | 3 | ▶ | croak "$_[0] is not habit|todo|daily|reward" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | unless $_[0] =~ /^(?:habit|todo|daily|reward)$/; | ||||||||||||||||||||
| 43 | 5 | }; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 12 | ▶ | my $NonEmpty = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 46 | 12 | ▶ | croak "Empty or undef parameter" unless length($_[0] // ""); | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_[0] // ""
MC/DC100%1/1
$_[0] // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||
| 47 | 5 | }; | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 5 | has 'text' => ( is => 'ro', required => 1, isa => $NonEmpty); | |||||||||||||||||||
| 50 | 5 | has 'id' => ( is => 'ro', required => 1, isa => $NonEmpty); | |||||||||||||||||||
| 51 | 1 | ▶ | has 'up' => ( is => 'ro', default => sub { 0 }, isa => $Bool); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 1 | ▶ | has 'down' => ( is => 'ro', default => sub { 0 }, isa => $Bool); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 5 | has 'value' => ( is => 'ro', required => 1, isa => $Num); | |||||||||||||||||||
| 54 | 5 | has 'type' => ( is => 'ro', required => 1, isa => $Type); | |||||||||||||||||||
| 55 | 5 | has 'history' => ( is => 'ro' ); # TODO: Objectify | |||||||||||||||||||
| 56 | 5 | has 'repeat' => ( is => 'ro' ); # TODO: Objectify | |||||||||||||||||||
| 57 | 5 | has 'completed' => ( is => 'ro' ); | |||||||||||||||||||
| 58 | 5 | has 'tags' => ( is => 'ro' ); # Hashref to uuid => True pairs. | |||||||||||||||||||
| 59 | 5 | has 'streak' => ( is => 'ro' ); | |||||||||||||||||||
| 60 | 5 | has '_raw' => ( is => 'rw' ); | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # Debugging hooks in case things go weird. | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 7 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.9
| |||||||||||||||||||||
| 65 | 7 | my $orig = shift; | |||||||||||||||||||
| 66 | 7 | my $class = shift; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 7 | ▶ | if ($WebService::HabitRPG::DEBUG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | warn "Building task with:\n"; | |||||||||||||||||||
| 70 | 0 | warn Dumper(\@_), "\n"; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 7 | return $class->$orig(@_); | |||||||||||||||||||
| 74 | 5 | }; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 2 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | 2 | my ($self, $args) = @_; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # Since we're usually being called directly with the results of | ||||||||||||||||||||
| 80 | # a JSON parse, we want to record that original structure here. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 2 | $self->_raw($args); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub active_today { | ||||||||||||||||||
|
Subroutine
active_today: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 87 | 0 | my ($self) = @_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | return 1 if $self->type ne 'daily'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | my $frequency = $self->_raw->{frequency}; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | if ($frequency eq 'weekly') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 94 | 0 | ▶ | return unless $self->repeat; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | my $today_short = (HRPG_REPEAT_MAP)[ DateTime->now->set_time_zone('local')->day_of_week - 1 ]; | |||||||||||||||||||
| 97 | 0 | return $self->repeat->{$today_short}; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | elsif ($frequency eq 'daily') { | ||||||||||||||||||||
| 100 | 0 | my $every_x = $self->_raw->{everyX}; | |||||||||||||||||||
| 101 | 0 | ▶ | return unless $every_x; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | my $start_date = DateTime::Format::ISO8601->new->parse_datetime($self->_raw->{startDate})->truncate(to => 'day'); | |||||||||||||||||||
| 104 | 0 | my $days_since_start = DateTime->today->delta_days($start_date)->in_units('days'); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | return $days_since_start % $every_x == 0; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||
| 109 | 0 | return; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 1 | ▶ | sub format_task { | ||||||||||||||||||
|
Subroutine
format_task: called CC=7 SCAR=26.3
Pod
documented
| |||||||||||||||||||||
| 115 | 1 | my ($task) = @_; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 1 | my $formatted = ""; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 1 | ▶ | if ($task->type =~ /^(?:daily|todo)$/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 120 | 0 | ▶ | if ($task->completed) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 121 | 0 | $formatted .= '[X] '; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | elsif (not $task->active_today) { | ||||||||||||||||||||
| 124 | 0 | $formatted .= '[-] '; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||
| 127 | 0 | $formatted .= '[ ] '; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | elsif ($task->type eq 'habit') { | ||||||||||||||||||||
| 131 | 1 | $formatted .= ' '; | |||||||||||||||||||
| 132 | 1 | ▶ | $formatted .= $task->{up} ? "+" : " " ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 1 | ▶ | $formatted .= $task->{down} ? "- " : " " ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||
| 136 | 0 | $formatted .= " * "; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 1 | $formatted .= $task->text; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 1 | return $formatted; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 5 | 1; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||