BusyBird/SafeData.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 100% 46 / 46 CC 2 SCAR 1.7
CC
2
Cov
100%
array
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« BusyBird.pm ↑ Summary BusyBird/StatusStorage.pm »
1 package BusyBird::SafeData;
2 18 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Exporter 5.57 qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Data::Diver ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 our @EXPORT_OK = qw(safed);
9  
10 1609 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 1609 my ($class, $data) = @_;
12 1609 return bless \$data, $class;
13 }
14  
15 1609 ▶sub safed {
Subroutine safed: called CC=1 SCAR=0.0
Pod documented
16 1609 return __PACKAGE__->new(@_);
17 }
18  
19 95 ▶sub original {
Subroutine original: called CC=1 SCAR=0.0
Pod documented
20 95 return ${shift()};
21 }
22  
23 3503 ▶sub val {
Subroutine val: called CC=1 SCAR=0.0
Pod documented
24 3503 my ($self, @path) = @_;
25 3503 return scalar(Data::Diver::Dive($$self, map { "$_" } @path));
26 }
27  
28 81 ▶sub array {
Subroutine array: called CC=2 SCAR=6.9
Pod documented
29 81 my ($self, @path) = @_;
30 81 my $val = $self->val(@path);
31 81 ▶ if(ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 6 75 81
32 6 return @$val;
33 }else {
34 75 return ();
35 }
36 }
37  
38 18 1;
39  
40 =pod
41  
42 =head1 NAME
43  
44 BusyBird::SafeData - a wrapper of a complex data structure to access its internals safely
45  
46 =head1 SYNOPSIS
47  
48 use BusyBird::SafeData qw(safed);
49
50 my $data = {
51 foo => {
52 bar => [
53 0, 1, 2
54 ],
55 buzz => { hoge => 100 }
56 }
57 };
58
59 my $sd = safed($data);
60
61 $sd->original; ## => $data
62
63 $sd->val("foo", "bar", 1); ## => 1
64 $sd->val("foo", "buzz", "FOO"); ## => undef
65 $sd->val("foo", "quux", "hoge"); ## => undef (and no autovivification)
66 $sd->val("foo", "bar", "FOO"); ## => undef (and no exception thrown)
67 $sd->val("foo", "buzz", "hoge", "FOO"); ## => undef (and no exception thrown)
68
69 $sd->array("foo", "bar"); ## => (0, 1, 2)
70 $sd->array("foo", "buzz"); ## => ()
71 $sd->array("foo", "bar", 1); ## => ()
72  
73 =head1 DESCRIPTION
74  
75 L<BusyBird::SafeData> is a wrapper around a complex data structure to provide a safe way to access its internal data.
76  
77 =head1 EXPORTABLE FUNCTIONS
78  
79 The following function is exported only by request.
80  
81 =head2 $sd = safed($data)
82  
83 Same as C<< BusyBird::SafeData->new($data). >>
84  
85 =head1 CLASS METHODS
86  
87 =head2 $sd = BusyBird::SafeData->new($data)
88  
89 The constructor.
90  
91 C<$data> is any scalar. C<$sd> wraps the given C<$data>.
92  
93 =head1 OBJECT METHODS
94  
95 =head2 $data = $sd->original()
96  
97 Returns the original C<$data> that C<$sd> wraps.
98  
99 =head2 $val = $sd->val(@path)
100  
101 Return the value specified by C<@path> from C<$sd>.
102  
103 C<@path> is a list of indices/keys from the root of the C<$sd> down to the value you want.
104 If it cannot traverse C<@path> completely, it returns C<undef>.
105  
106 This method never autovivifies anything in C<$sd>.
107  
108 =head2 @vals = $sd->array(@path)
109  
110 Return the list of values in the array-ref specified by C<@path>.
111 If it cannot traverse C<@path> completely, it returns an empty list.
112 If the value at the C<@path> is not an array-ref, it returns an empty list.
113  
114 =head1 AUTHOR
115  
116 Toshio Ito C<< <toshioito [at] cpan.org> >>
117  
118 =cut
« BusyBird.pm ↑ Summary BusyBird/StatusStorage.pm »