Formula/Context.pm

filter: Statementstmt 92.8% 91 / 98 Branchbran 62.1% 41 / 66 Conditioncond 50.0% 19 / 38 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 95.6% 22 / 23 Podpod 93.3% 14 / 15 totaltotal 73.3% 193 / 263 SCAR 44.7
CC
49
Cov
75%
addFormula
23.6
_default
21.2
add
16.8
CRAP 87.6
« Formula/Config/INI.pm ↑ Summary Formula.pm »
1 # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5  
6 package Math::Formula::Context;
7 15 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 $VERSION = '0.16';
9  
10  
11 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 15 ▶use Log::Report 'math-formula';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 13 ▶sub new(%) { my $class = shift; (bless {}, $class)->init({@_}) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19  
20 sub _default($$$$)
21 52 ▶{ my ($self, $name, $type, $value, $default) = @_;
Subroutine _default: called CC=6 SCAR=30.5
22 52 ▶ my $form
Branch: 5 branches
BranchTrueFalseTotal
!$value ? : 52 0 52
!builtin::blessed($value) ? : 0 0 0
$value ? : 0 0 0
$value->isa('Math::Formula') ? : 0 0 0
$value->isa('Math::Formula::Type') ? : 0 0 0
23 = ! $value ? $type->new(undef, $default)
24 : ! blessed $value ? ($value ? Math::Formula->new($name, $value) : undef)
25 : $value->isa('Math::Formula') ? $value
26 : $value->isa('Math::Formula::Type') ? $value
27 : error __x"unexpected value for '{name}' in #{context}", name => $name, context => $self->name;
28 }
29  
30 sub init($)
31 13 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=7 SCAR=19.7
Pod undocumented
32 13 ▶ my $name = $args->{name} or error __x"context requires a name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $args->{"name"} or &error(&__x("context requires a name")) 0 13 13
33 13 ▶ my $node = blessed $name ? $name : MF::STRING->new(undef, $name);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($name) ? : 0 13 13
34 13 $self->{MFC_name} = $node->value;
35  
36 13 my $now;
37 $self->{MFC_attrs} = {
38 ctx_name => $node,
39 ctx_version => $self->_default(version => 'MF::STRING', $args->{version}, "1.00"),
40 ctx_created => $self->_default(created => 'MF::DATETIME', $args->{created}, $now = DateTime->now),
41 ctx_updated => $self->_default(updated => 'MF::DATETIME', $args->{updated}, $now //= DateTime->now),
42 13 ▶ ctx_mf_version => $self->_default(mf_version => 'MF::STRING', $args->{mf_version}, $Math::Formula::VERSION),
Condition33%1/3
$now //= "DateTime"->now
l!l&&r!l&&!r
1300
MC/DC0%0/2
$now //= "DateTime"->now
$now "DateTime"->now
Truth table33%1/3
A //= B
A= $nowB= "DateTime"->now
ABresult
1X1
011
000
43 };
44  
45 13 ▶ $self->{MFC_lead} = $args->{lead_expressions} // '';
Condition100%2/2
$args->{"lead_expressions"} // ""
l!l
112
MC/DC100%1/1
$args->{"lead_expressions"} // ""
$args->{"lead_expressions"}
Truth table100%2/2
A
A= $args->{"lead_expressions"}
Aresult
11
00
46 13 $self->{MFC_forms} = { };
47 13 $self->{MFC_frags} = { };
48 13 ▶ if(my $forms = $args->{formulas})
Branch: 1 branch
BranchTrueFalseTotal
if (my $forms = $args->{"formulas"}) 2 11 13
49 2 ▶ { $self->add(ref $forms eq 'ARRAY' ? @$forms : $forms);
Branch: 1 branch
BranchTrueFalseTotal
ref $forms eq 'ARRAY' ? : 1 1 2
50 }
51  
52 13 $self->{MFC_claims} = { };
53 13 $self->{MFC_capts} = [ ];
54 13 $self;
55 }
56  
57 # For save()
58 sub _index()
59 0 ▶{ my $self = shift;
Subroutine _index: not called CC=1 SCAR=6.9
60 +{ attributes => $self->{MFC_attrs},
61 formulas => $self->{MFC_forms},
62 fragments => $self->{MFC_frags},
63 0 };
64 }
65  
66 #--------------
67  
68 11 ▶sub name { $_[0]->{MFC_name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
69 14 ▶sub lead_expressions { $_[0]->{MFC_lead} }
Subroutine lead_expressions: called CC=1 SCAR=0.0
Pod documented
70  
71 #--------------
72  
73 sub attribute($)
74 4 ▶{ my ($self, $name) = @_;
Subroutine attribute: called CC=2 SCAR=7.1
Pod documented
75 4 ▶ my $def = $self->{MFC_attrs}{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->{"MFC_attrs"}{$name} or return 0 4 4
76 4 Math::Formula->new($name => $def);
77 }
78  
79 #--------------
80 #XXX example with fragment
81  
82 sub add(@)
83 17 ▶{ my $self = shift;
Subroutine add: called CC=10 SCAR=28.2
Pod documented
84 17 ▶ unless(ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 13 4 17
85 13 { my $name = shift;
86 13 ▶ return $name =~ s/^#// ? $self->addFragment($name, @_) : $self->addFormula($name, @_);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^#// ? : 0 13 13
87 }
88  
89 4 foreach my $obj (@_)
90 6 ▶ { if(ref $obj eq 'HASH')
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'HASH') { } 4 2 6
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula')) { } 2 0 2
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula::Context')) { } 0 0 0
91 4 { $self->add($_, $obj->{$_}) for keys %$obj;
92 }
93 ▶ elsif(blessed $obj && $obj->isa('Math::Formula'))
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Math::Formula")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Math::Formula")
builtin::blessed($obj) $obj->isa("Math::Formula")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Math::Formula")
ABresult
0X0
100
111
94 2 { $self->{MFC_forms}{$obj->name} = $obj;
95 }
96 ▶ elsif(blessed $obj && $obj->isa('Math::Formula::Context'))
Condition0%0/3
builtin::blessed($obj) and $obj->isa("Math::Formula::Context")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Math::Formula::Context")
builtin::blessed($obj) $obj->isa("Math::Formula::Context")
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Math::Formula::Context")
ABresult
0X0
100
111
97 0 { $self->{MFC_frags}{$obj->name} = $obj;
98 }
99 else
100 0 { panic __x"formula add '{what}' not understood", what => $obj;
101 }
102 }
103  
104 4 undef;
105 }
106  
107  
108 sub addFormula(@)
109 23 ▶{ my ($self, $name) = (shift, shift);
Subroutine addFormula: called CC=19 SCAR=31.6
Pod documented
110 23 my $next = $_[0];
111 23 my $forms = $self->{MFC_forms};
112  
113 23 ▶ if(ref $name)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $name) { } 1 22 23
elsif (not ref $name and @_) { } 22 0 22
114 1 ▶ { return $forms->{$name->name} = $name
Branch: 1 branch
BranchTrueFalseTotal
if not @_ and builtin::blessed($name) and $name->isa("Math::Formula") 1 0 1
Condition33%2/6
not @_ and builtin::blessed($name) and $name->isa("Math::Formula")
!ll&&!rl&&r
001
not @_ and builtin::blessed($name)
!ll&&!rl&&r
001
MC/DC0%0/3
not @_ and builtin::blessed($name) and $name->isa("Math::Formula")
not @_ builtin::blessed($name) $name->isa("Math::Formula")
Truth table25%1/4
not(A) and B and C
A= not @_B= builtin::blessed($name)C= $name->isa("Math::Formula")
ABCresult
0XX0
10X0
1100
1111
115 if !@_ && blessed $name && $name->isa('Math::Formula');
116 }
117 ▶ elsif(! ref $name && @_)
Condition33%1/3
not ref $name and @_
!ll&&!rl&&r
0022
MC/DC0%0/2
not ref $name and @_
not ref $name @_
Truth table33%1/3
not(A) and B
A= not ref $nameB= @_
ABresult
0X0
100
111
118 22 ▶ { return $forms->{$name} = $next
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula") 3 19 22
Condition83%5/6
@_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula")
!ll&&!rl&&r
1903
@_ == 1 and builtin::blessed($next)
!ll&&!rl&&r
1183
MC/DC66%2/3
@_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula")
@_ == 1 builtin::blessed($next) $next->isa("Math::Formula")
Truth table75%3/4
A and B and C
A= @_ == 1B= builtin::blessed($next)C= $next->isa("Math::Formula")
ABCresult
0XX0
10X0
1100
1111
119 if @_==1 && blessed $next && $next->isa('Math::Formula');
120  
121 19 ▶ return $forms->{$name} = Math::Formula->new($name, @_)
Branch: 1 branch
BranchTrueFalseTotal
if ref $next eq "CODE" 1 18 19
122 if ref $next eq 'CODE';
123  
124 18 ▶ return $forms->{$name} = Math::Formula->new($name, @_)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($next) and $next->isa("Math::Formula::Type") 0 18 18
Condition33%1/3
builtin::blessed($next) and $next->isa("Math::Formula::Type")
!ll&&!rl&&r
1800
MC/DC0%0/2
builtin::blessed($next) and $next->isa("Math::Formula::Type")
builtin::blessed($next) $next->isa("Math::Formula::Type")
Truth table33%1/3
A and B
A= builtin::blessed($next)B= $next->isa("Math::Formula::Type")
ABresult
0X0
100
111
125 if blessed $next && $next->isa('Math::Formula::Type');
126  
127 18 ▶ my ($data, %attrs) = @_==1 && ref $next eq 'ARRAY' ? @$next : $next;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $next eq 'ARRAY' ? : 4 14 18
Condition100%3/3
@_ == 1 and ref $next eq "ARRAY"
!ll&&!rl&&r
1134
MC/DC100%2/2
@_ == 1 and ref $next eq "ARRAY"
@_ == 1 ref $next eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $next eq "ARRAY"
ABresult
0X0
100
111
128 18 ▶ if(my $r = $attrs{returns})
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $attrs{"returns"}) 4 14 18
129 4 ▶ { my $typed = $r->isa('MF::STRING') ? $r->new(undef, $data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$r->isa('MF::STRING') ? : 0 4 4
130 4 return $forms->{$name} = Math::Formula->new($name, $typed, %attrs);
131 }
132  
133 14 ▶ if(length(my $leader = $self->lead_expressions))
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $leader = $self->lead_expressions)) 3 11 14
134 3 ▶ { my $typed = $data =~ s/^\Q$leader// ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s/^\Q$leader\E// ? : 2 1 3
135 3 return $forms->{$name} = Math::Formula->new($name, $typed, %attrs);
136 }
137  
138 11 return $forms->{$name} = Math::Formula->new($name, $data, %attrs);
139 }
140  
141 0 error __x"formula declaration '{name}' not understood", name => $name;
142 }
143  
144  
145 49 ▶sub formula($) { $_[0]->{MFC_forms}{$_[1]} }
Subroutine formula: called CC=1 SCAR=0.0
Pod documented
146  
147  
148 sub addFragment($;$)
149 1 ▶{ my $self = shift;
Subroutine addFragment: called CC=2 SCAR=7.1
Pod documented
150 1 ▶ my ($name, $fragment) = @_==2 ? @_ : ($_[0]->name, $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
151 1 $self->{MFC_frags}{$name} = MF::FRAGMENT->new($name, $fragment);
152 }
153  
154  
155 7 ▶sub fragment($) { $_[0]->{MFC_frags}{$_[1]} }
Subroutine fragment: called CC=1 SCAR=0.0
Pod documented
156  
157 #-------------------
158  
159 sub evaluate($$%)
160 28 ▶{ my ($self, $name) = (shift, shift);
Subroutine evaluate: called CC=5 SCAR=17.0
Pod documented
161  
162 # Wow, I am impressed! Caused by prefix(#,.) -> infix
163 28 ▶ length $name or return $self;
Branch: 1 branch
BranchTrueFalseTotal
length $name or return $self 0 28 28
164  
165 28 ▶ my $form = $name =~ /^ctx_/ ? $self->attribute($name) : $self->formula($name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^ctx_/ ? : 3 25 28
166 28 ▶ unless($form)
Branch: 1 branch
BranchTrueFalseTotal
unless ($form) 0 28 28
167 0 { warning __x"no formula '{name}' in {context}", name => $name, context => $self->name;
168 0 return undef;
169 }
170  
171 28 my $claims = $self->{MFC_claims};
172 28 ▶ ! $claims->{$name}++
Branch: 1 branch
BranchTrueFalseTotal
$claims->{$name}++ and &error(&__x("recursion in expression '{name}' at {context}", "name", $name, "context", $self->name)) 0 28 28
173 or error __x"recursion in expression '{name}' at {context}",
174 name => $name, context => $self->name;
175  
176 28 my $result = $form->evaluate($self, @_);
177  
178 28 delete $claims->{$name};
179 28 $result;
180 }
181  
182  
183 sub run($%)
184 21 ▶{ my ($self, $expr, %args) = @_;
Subroutine run: called CC=4 SCAR=14.2
Pod documented
185 21 ▶ my $name = delete $args{name} || join '#', (caller)[1,2];
Condition33%1/3
delete $args{'name'} || join('#', (caller)[1, 2])
l!l&&r!l&&!r
0210
MC/DC0%0/2
delete $args{'name'} || join('#', (caller)[1, 2])
delete $args{'name'} join('#', (caller)[1, 2])
Truth table33%1/3
A || B
A= delete $args{'name'}B= join('#', (caller)[1, 2])
ABresult
1X1
011
000
186 21 my $result = Math::Formula->new($name, $expr)->evaluate($self, %args);
187  
188 21 ▶ while($result && $result->isa('MF::NAME'))
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $result->isa("MF::NAME")) 6 21 27
Condition66%2/3
$result and $result->isa("MF::NAME")
!ll&&!rl&&r
0216
MC/DC50%1/2
$result and $result->isa("MF::NAME")
$result $result->isa("MF::NAME")
Truth table66%2/3
A and B
A= $resultB= $result->isa("MF::NAME")
ABresult
0X0
100
111
189 6 { $result = $self->evaluate($result->token, %args);
190 }
191  
192 21 $result;
193 }
194  
195  
196 sub value($@)
197 12 ▶{ my $self = shift;
Subroutine value: called CC=2 SCAR=7.1
Pod documented
198 12 my $result = $self->run(@_);
199 12 ▶ $result ? $result->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 12 0 12
200 }
201  
202  
203 10 ▶sub setCaptures($) { $_[0]{MFC_capts} = $_[1] }
Subroutine setCaptures: called CC=1 SCAR=0.0
Pod documented
204 7 ▶sub _captures() { $_[0]{MFC_capts} }
Subroutine _captures: called CC=1 SCAR=0.0
205  
206  
207 4 ▶sub capture($) { $_[0]->_captures->[$_[1]] }
Subroutine capture: called CC=1 SCAR=0.0
Pod documented
208  
209 #--------------
210  
211 15 1;
« Formula/Config/INI.pm ↑ Summary Formula.pm »