Mojo/JSON/Pointer.pm

filter: Statementstmt 100% 19 / 19 Branchbran 100% 16 / 16 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 3 / 3 totaltotal 100% 57 / 57 CC 13 SCAR 21.8
CC
13
Cov
100%
_pointer
12.0
new
2.0
BEGIN
1.0
CRAP 8.9
« Mojo/WebSocket.pm ↑ Summary Mojolicious/Plugin/NotYAMLConfig.pm »
1 package Mojo::JSON::Pointer;
2 30 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 has 'data';
5  
6 16 ▶sub contains { shift->_pointer(0, @_) }
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
7 41 ▶sub get { shift->_pointer(1, @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
8  
9 30 ▶sub new { @_ > 1 ? shift->SUPER::new(data => shift) : shift->SUPER::new }
Subroutine new: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 26 4 30
10  
11 57 ▶sub _pointer {
Subroutine _pointer: called CC=12 SCAR=24.8
12 57 my ($self, $get, $pointer) = @_;
13  
14 57 my $data = $self->data;
15 57 ▶ return length $pointer ? undef : $get ? $data : 1 unless $pointer =~ s!^/!!;
Branch: 3 branches
BranchTrueFalseTotal
unless $pointer =~ s[^/][]u 10 47 57
length $pointer ? : 2 8 10
$get ? : 4 4 8
16 47 ▶ for my $p (length $pointer ? (split /\//, $pointer, -1) : ($pointer)) {
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 41 6 47
17 74 $p =~ s!~1!/!g;
18 74 $p =~ s/~0/~/g;
19  
20 # Hash
21 74 ▶ if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH' and exists $data->{$p}) { } 53 21 74
elsif (ref $data eq 'ARRAY' and $p =~ /^\d+$/u and @$data > $p) { } 11 10 21
Condition100%3/3
ref $data eq "HASH" and exists $data->{$p}
!ll&&!rl&&r
18353
MC/DC100%2/2
ref $data eq "HASH" and exists $data->{$p}
ref $data eq "HASH" exists $data->{$p}
Truth table100%3/3
A and B
A= ref $data eq "HASH"B= exists $data->{$p}
ABresult
0X0
100
111
22  
23 # Array
24 11 ▶ elsif (ref $data eq 'ARRAY' && $p =~ /^\d+$/ && @$data > $p) { $data = $data->[$p] }
Condition100%6/6
ref $data eq "ARRAY" and $p =~ /^\d+$/u and @$data > $p
!ll&&!rl&&r
8211
ref $data eq "ARRAY" and $p =~ /^\d+$/u
!ll&&!rl&&r
7113
MC/DC100%3/3
ref $data eq "ARRAY" and $p =~ /^\d+$/u and @$data > $p
ref $data eq "ARRAY" $p =~ /^\d+$/u @$data > $p
Truth table100%4/4
A and B and C
A= ref $data eq "ARRAY"B= $p =~ /^\d+$/uC= @$data > $p
ABCresult
0XX0
10X0
1100
1111
25  
26 # Nothing
27 10 else { return undef }
28 }
29  
30 37 ▶ return $get ? $data : 1;
Branch: 1 branch
BranchTrueFalseTotal
$get ? : 33 4 37
31 }
32  
33 30 1;
34  
35 =encoding utf8
36  
37 =head1 NAME
38  
39 Mojo::JSON::Pointer - JSON Pointers
40  
41 =head1 SYNOPSIS
42  
43 use Mojo::JSON::Pointer;
44  
45 my $pointer = Mojo::JSON::Pointer->new({foo => [23, 'bar']});
46 say $pointer->get('/foo/1');
47 say 'Contains "/foo".' if $pointer->contains('/foo');
48  
49 =head1 DESCRIPTION
50  
51 L<Mojo::JSON::Pointer> is an implementation of L<RFC 6901|https://tools.ietf.org/html/rfc6901>.
52  
53 =head1 ATTRIBUTES
54  
55 L<Mojo::JSON::Pointer> implements the following attributes.
56  
57 =head2 data
58  
59 my $data = $pointer->data;
60 $pointer = $pointer->data({foo => 'bar'});
61  
62 Data structure to be processed.
63  
64 =head1 METHODS
65  
66 L<Mojo::JSON::Pointer> inherits all methods from L<Mojo::Base> and implements the following new ones.
67  
68 =head2 contains
69  
70 my $bool = $pointer->contains('/foo/1');
71  
72 Check if L</"data"> contains a value that can be identified with the given JSON Pointer.
73  
74 # True
75 Mojo::JSON::Pointer->new('just a string')->contains('');
76 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/♥');
77 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/foo');
78 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/1');
79  
80 # False
81 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/☃');
82 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/bar');
83 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/9');
84  
85 =head2 get
86  
87 my $value = $pointer->get('/foo/bar');
88  
89 Extract value from L</"data"> identified by the given JSON Pointer.
90  
91 # "just a string"
92 Mojo::JSON::Pointer->new('just a string')->get('');
93  
94 # "mojolicious"
95 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->get('/♥');
96  
97 # "bar"
98 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/foo');
99  
100 # "4"
101 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/0');
102  
103 # "6"
104 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/2');
105  
106 =head2 new
107  
108 my $pointer = Mojo::JSON::Pointer->new;
109 my $pointer = Mojo::JSON::Pointer->new({foo => 'bar'});
110  
111 Build new L<Mojo::JSON::Pointer> object.
112  
113 =head1 SEE ALSO
114  
115 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
116  
117 =cut
« Mojo/WebSocket.pm ↑ Summary Mojolicious/Plugin/NotYAMLConfig.pm »