TPath/Math.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« TPath/Selector/Predicated.pm ↑ Summary TPath/Selector/Test/AnywhereAttribute.pm »
1 package TPath::Math;
2 $TPath::Math::VERSION = '1.007';
3 # ABSTRACT: handles the arithmetic in C<//foo[1 + @bar = @quux]>
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use TPath::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use namespace::autoclean;
9  
10 with qw(TPath::Numifiable);
11  
12 has operator => ( is => 'ro', isa => 'Str', required => 1 );
13  
14 has func => ( is => 'ro', isa => 'CodeRef', writer => '_func' );
15  
16 has args => ( is => 'ro', isa => 'ArrayRef[MathArg]' );
17  
18 sub BUILD {
19 my $self = shift;
20 my $sub = eval 'sub { $_[0] ' . $self->operator . ' $_[1] }';
21 $self->_func(sub { reduce { $sub->($a, $b) } @_ });
22 }
23  
24 sub to_string {
25 my ($self, $in_parens) = @_;
26 my $s = $in_parens ? '' : '(';
27 my $non_first = 0;
28 for my $arg (@{$self->args}) {
29 $s .= $self->operator if $non_first++;
30 $s .= ' ';
31 $s .= ref $arg ? $arg->to_string : $arg;
32 $s .= ' ';
33 }
34 if ($in_parens) {
35 $s =~ s/^\s+|\s+$//g;
36 } else {
37 $s .= ')';
38 }
39 return $s;
40 }
41  
42 sub to_num {
43 my ($self, $ctx) = @_;
44 my @args = map { ref $_ ? $_->to_num($ctx) : $_ } @{$self->args};
45 return $self->func->(@args);
46 }
47  
48 1;
49  
50 __END__
« TPath/Selector/Predicated.pm ↑ Summary TPath/Selector/Test/AnywhereAttribute.pm »