blib/lib/Froody/Walker/Terse.pm

filter: Statementstmt 100% 76 / 76 Branchbran 83.3% 25 / 30 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 10 / 10 totaltotal 96.4% 134 / 139 CC 21 SCAR 13.2
CC
21
Cov
95%
validate_source
7.0
text_only_spec
5.0
init_target
5.0
CRAP 3.8
« blib/lib/Froody/Server/Standalone.pm ↑ Summary blib/lib/Froody/Invoker/PluginService.pm »
1 package Froody::Walker::Terse;
2 12 ▶use base 'Froody::Walker::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 my $logger = get_logger('froody.walker.terse');
11  
12 =head1 NAME
13  
14 Froody::Walker::Terse - read and write Terse data.
15  
16 =head1 SYNOPSIS
17  
18 =head1 DESCRIPTION
19  
20 Turn what Implementation class returns into the structure
21 L<Froody::Response> expects.
22  
23 =over
24  
25 =cut
26  
27 =item text_only_spec( path )
28  
29 returns true if the spec at this location specifies that there
30 is _only_ text content in this node.
31  
32 =back
33  
34 =cut
35  
36 100 ▶sub text_only_spec {
Subroutine text_only_spec: called CC=5 SCAR=16.2
Pod documented
37 100 my ($self, $path) = @_;
38 100 my $spec = $self->spec_for_xpath($path);
39 100 ▶ return 1 unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 100 100
40 100 ▶ warn "No spec for $path" unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 100 100
41 100 ▶ return 0 if (@{$spec->{elts}} || @{$spec->{attr}});
Branch: 1 branch
BranchTrueFalseTotal
if @{$spec->{"elts"};} or @{$spec->{"attr"};} 29 71 100
Condition100%3/3
@{$spec->{"elts"};} or @{$spec->{"attr"};}
l!l&&r!l&&!r
82171
MC/DC100%2/2
@{$spec->{"elts"};} or @{$spec->{"attr"};}
@{$spec->{"elts"};} @{$spec->{"attr"};}
Truth table100%3/3
A or B
A= @{$spec->{"elts"};}B= @{$spec->{"attr"};}
ABresult
1X1
011
000
42 71 return 1;
43 }
44  
45  
46  
47 49 ▶sub init_source {
Subroutine init_source: called CC=1 SCAR=0.0
Pod documented
48 49 my ($self, $source) = @_;
49 49 return $source;
50 }
51  
52 159 ▶sub init_target {
Subroutine init_target: called CC=5 SCAR=16.1
Pod documented
53 159 my ($self, $path, $parent) = @_;
54 159 my $target = {};
55 # the target should have listref slots where we're expecting lists,
56 # in case no elements get added to those lists later.
57 159 ▶ if (my $spec = $self->spec_for_xpath($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spec = $self->spec_for_xpath($path)) 158 1 159
58 158 for (@{ $spec->{elts} }) {
59 # initialize an empty list if we're expecting a list at all
60 95 my $elt_spec = $self->spec_for_xpath( $path ."/". $_ );
61 95 ▶ next unless $elt_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $elt_spec 0 95 95
62 95 ▶ $target->{$_} = [] if $elt_spec->{multi};
Branch: 1 branch
BranchTrueFalseTotal
if $elt_spec->{"multi"} 27 68 95
63 }
64 }
65 159 return $target;
66 }
67  
68 130 ▶sub validate_source {
Subroutine validate_source: called CC=7 SCAR=19.5
Pod documented
69 130 my ($self, $source, $path) = @_;
70
71 #use Carp; Carp::cluck;
72
73 130 my $spec = $self->spec_for_xpath($path);
74 130 ▶ my $method = $self->walker->method || '<unknown>';
Condition100%2/2
$self->walker->method || '<unknown>'
l!l
1237
MC/DC100%1/1
$self->walker->method || '<unknown>'
$self->walker->method
Truth table100%2/2
A
A= $self->walker->method
Aresult
11
00
75
76 130 ▶ if (!ref $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $source) 58 72 130
77 $logger->warn("Returned unexpected text at '$path' for structure '$method'.")
78 58 ▶ unless $spec->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"text"} 0 58 58
79 58 return 1;
80 }
81
82 72 my %keys = map { $_ => 1 } keys %$source;
83
84 72 foreach (@{ $spec->{attr} } , @{$spec->{elts}}) {
85 170 delete $keys{$_};
86 }
87 72 ▶ delete $keys{-text} if $spec->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"text"} 26 46 72
88
89 72 for (keys %keys) {
90 2 $logger->warn("unknown key '$_' defined within a terse structure at '$path' for structure '$method'.");
91 }
92 72 return !keys %keys;
93 }
94  
95 94 ▶sub read_attribute {
Subroutine read_attribute: called CC=2 SCAR=7.2
Pod documented
96 94 my ($self, $source, $path, $attr) = @_;
97 94 ▶ return ref($source) ? $source->{$attr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $source ? : 94 0 94
98 }
99  
100 84 ▶sub read_text {
Subroutine read_text: called CC=2 SCAR=6.9
Pod documented
101 84 my ($self, $source, $xpath_key) = @_;
102 84 ▶ return ref $source ? $source->{-text} : $source;
Branch: 1 branch
BranchTrueFalseTotal
ref $source ? : 26 58 84
103 }
104  
105 76 ▶sub child_sources {
Subroutine child_sources: called CC=3 SCAR=11.0
Pod documented
106 76 my ($self, $source, $xpath, $element) = @_;
107
108 76 my $stuff = $source->{$element};
109 76 ▶ $stuff = [] unless defined $stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stuff 20 56 76
110
111 76 ▶ return @{ $stuff } if ref $stuff eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $stuff eq "ARRAY" 38 38 76
112 38 return ( $stuff );
113 }
114  
115 115 ▶sub write_attribute {
Subroutine write_attribute: called CC=1 SCAR=0.0
Pod documented
116 115 my ($self, $target, $path, $attr, $value) = @_;
117 115 $target->{$attr} = $value;
118 115 return $target;
119 }
120  
121 100 ▶sub write_text {
Subroutine write_text: called CC=2 SCAR=6.9
Pod documented
122 100 my ($self, $target, $path, $value) = @_;
123 100 ▶ if ($self->text_only_spec($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->text_only_spec($path)) { } 71 29 100
124 71 return $value;
125 } else {
126 29 $target->{-text} = $value;
127 29 return $target;
128 }
129 }
130  
131  
132 103 ▶sub add_child_to_target {
Subroutine add_child_to_target: called CC=2 SCAR=6.9
Pod documented
133 103 my ($self, $target, $xpath, $element, $child) = @_;
134 103 ▶ if (ref ($target->{$element}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target->{$element} eq 'ARRAY') { } 55 48 103
135 55 push @{ $target->{$element} }, $child;
136 } else {
137 48 $target->{$element} = $child;
138 }
139 103 return $target;
140 }
141  
142 =head1 BUGS
143  
144 None known.
145  
146 Please report any bugs you find via the CPAN RT system.
147 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
148  
149 =head1 AUTHOR
150  
151 Copyright Fotango 2005. All rights reserved.
152  
153 Please see the main L<Froody> documentation for details of who has worked
154 on this project.
155  
156 This module is free software; you can redistribute it and/or modify it under
157 the same terms as Perl itself.
158  
159 =head1 SEE ALSO
160  
161 L<Froody>, L<Froody::Walker>
162  
163 =cut
164  
165 12 1;
« blib/lib/Froody/Server/Standalone.pm ↑ Summary blib/lib/Froody/Invoker/PluginService.pm »