| 1 | package Dallycot::Value::Duration; | ||||||||||||||||||||||||||||
| 2 | 30 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Date and time values | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use experimental qw(switch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 30 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 30 | ▶ | use parent 'Dallycot::Value::Any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 30 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 30 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 30 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 30 | ▶ | use Promises qw(deferred); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub new { | ||||||||||||||||||||||||||||
| 22 | 36 | ▶ | my($class, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 36 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 36 | for my $k (keys %options) { | |||||||||||||||||||||||||||
| 27 | 130 | ▶ | $options{$k} = $options{$k} -> value -> numify if blessed($options{$k}) && $options{$k}->isa('Dallycot::Value::Numeric'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($options{$k}) and $options{$k}->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
builtin::blessed($options{$k}) and $options{$k}->isa("Dallycot::Value::Numeric")
builtin::blessed($options{$k})
$options{$k}->isa("Dallycot::Value::Numeric")
Truth table66%2/3
A and B
A= builtin::blessed($options{$k})B= $options{$k}->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 36 | my $duration_class = delete $options{'class'}; | |||||||||||||||||||||||||||
| 31 | 36 | ▶ | $duration_class //= 'DateTime::Duration'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$duration_class //= "DateTime::Duration"
MC/DC100%1/1
$duration_class //= "DateTime::Duration"
$duration_class
Truth table100%2/2
A
A= $duration_class
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 36 | ▶ | if($options{object}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | return bless [ | ||||||||||||||||||||||||||||
| 35 | $options{object}->clone | ||||||||||||||||||||||||||||
| 36 | 10 | ] => $class; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||
| 39 | return bless [ | ||||||||||||||||||||||||||||
| 40 | $duration_class->new( | ||||||||||||||||||||||||||||
| 41 | 26 | map { $_ => $options{$_} } grep { $options{$_} } keys %options | |||||||||||||||||||||||||||
| 42 | ) | ||||||||||||||||||||||||||||
| 43 | ] => $class; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub to_rdf { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my($self, $model) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_rdf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # we need to record the date/time as represented in RDF, but might want to | ||||||||||||||||||||||||||||
| 51 | # record the calendar type as well | ||||||||||||||||||||||||||||
| 52 | 0 | my $literal = RDF::Trine::Node::Literal->new( | |||||||||||||||||||||||||||
| 53 | $self -> as_text, | ||||||||||||||||||||||||||||
| 54 | '', | ||||||||||||||||||||||||||||
| 55 | $model -> meta_uri('xsd:duration') | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | return $literal; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub as_text { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_text: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if($self -> [0] -> is_zero) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return 'P0Y'; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my %amounts; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $duration = $self -> [0]; | |||||||||||||||||||||||||||
| 71 | 0 | my $string = ''; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if($duration -> is_negative) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $duration = $duration -> clone -> inverse; | |||||||||||||||||||||||||||
| 74 | 0 | $string = '-'; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | $string .= 'P'; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | @amounts{qw(Y M D h m s)} = $duration -> in_units('years', 'months', 'days', 'hours', 'minutes', 'seconds'); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my $days = join("", | ||||||||||||||||||||||||||||
| 81 | 0 | map { $amounts{$_} . $_ } | |||||||||||||||||||||||||||
| 82 | 0 | grep { $amounts{$_} > 0 } | |||||||||||||||||||||||||||
| 83 | qw(Y M D) | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | my $hours = join("", | ||||||||||||||||||||||||||||
| 87 | 0 | map { $amounts{$_} . uc($_) } | |||||||||||||||||||||||||||
| 88 | 0 | grep { $amounts{$_} > 0 } | |||||||||||||||||||||||||||
| 89 | qw(h m s) | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | $string .= $days; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $string .= 'T' . $hours if $hours ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return $string; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub value { | ||||||||||||||||||||||||||||
| 100 | 35 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 35 | return $self->[0]; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub negated { | ||||||||||||||||||||||||||||
| 106 | 1 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
negated: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | return $self->new( object => $self->[0] -> inverse ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub is_equal { | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my ( $self, $engine, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $d->resolve( 0 == DateTime::Duration->compare( $self->value, $other->value ) ); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub is_less { | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my ( $self, $engine, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_less: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | $d->resolve( 0 > DateTime::Duration->compare( $self->value, $other->value ) ); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub is_less_or_equal { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my ( $self, $engine, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_less_or_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $d->resolve( 0 >= DateTime::Duration->compare( $self->value, $other->value ) ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub is_greater { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my ( $self, $engine, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_greater: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | $d->resolve( 0 < DateTime::Duration->compare( $self->value, $other->value ) ); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub is_greater_or_equal { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ( $self, $engine, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_greater_or_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $d->resolve( 0 <= DateTime::Duration->compare( $self->value, $other->value ) ); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub prepend { | ||||||||||||||||||||||||||||
| 162 | 2 | ▶ | my( $self, @things ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prepend: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # if durations, we add | ||||||||||||||||||||||||||||
| 165 | # if numeric, we add as seconds | ||||||||||||||||||||||||||||
| 166 | # otherwise, we can't prepend | ||||||||||||||||||||||||||||
| 167 | 2 | ▶ | if(any { !$_->isa('Dallycot::Value::Numeric') && !$_->isa('Dallycot::Value::Duration') } @things) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
Branch: 1 branch
Condition66%2/3
$_->isa('Dallycot::Value::Numeric') || $_->isa('Dallycot::Value::Duration')
MC/DC0%0/2
$_->isa('Dallycot::Value::Numeric') || $_->isa('Dallycot::Value::Duration')
$_->isa('Dallycot::Value::Numeric')
$_->isa('Dallycot::Value::Duration')
Truth table66%2/3
A || B
A= $_->isa('Dallycot::Value::Numeric')B= $_->isa('Dallycot::Value::Duration')
| |||||||||||||||||||||||||||||
| 168 | 0 | croak 'Only durations and numeric values may be added to durations'; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | my @durations = map { | ||||||||||||||||||||||||||||
| 172 | 2 | my $v = $_; | |||||||||||||||||||||||||||
| 173 | 2 | given(blessed $v) { | |||||||||||||||||||||||||||
| 174 | 2 | when('Dallycot::Value::Numeric') { | |||||||||||||||||||||||||||
| 175 | 1 | DateTime::Duration->new( | |||||||||||||||||||||||||||
| 176 | seconds => $v->value->numify | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 1 | when('Dallycot::Value::Duration') { | |||||||||||||||||||||||||||
| 180 | 1 | $v -> value; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } @things; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | my $accumulator = $self->value; | |||||||||||||||||||||||||||
| 186 | 2 | $accumulator = $accumulator + $_ for @durations; | |||||||||||||||||||||||||||
| 187 | 2 | return $self -> new(object => $accumulator, class => blessed($accumulator)); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 30 | 1; | |||||||||||||||||||||||||||