blib/lib/Dir/Manifest.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 96.7% 118 / 122 SCAR 20.8
CC
8
Cov
96%
_is_valid_key
4.0
remove_key
2.0
add_key
2.0
CRAP 8.0
« Build ↑ Summary blib/lib/Dir/Manifest/Slurp.pm »
1 package Dir::Manifest;
2 2 $Dir::Manifest::VERSION = '0.4.0';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Path::Tiny qw/ path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Dir::Manifest::Key ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Dir::Manifest::Slurp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 has 'manifest_fn' => ( is => 'ro', required => 1 );
15 2 has 'dir' => ( is => 'ro', required => 1 );
16  
17 2 my $ALLOWED = qr/[a-zA-Z0-9_\-\.=]/;
18 2 my $ALPHAN = qr/[a-zA-Z0-9_]/;
19  
20 sub _is_valid_key
21 {
22 16 ▶ my ( $self, $key ) = @_;
Subroutine _is_valid_key: called CC=4 SCAR=13.9
23 16 ▶ if ( $key !~ /\A(?:$ALLOWED)+\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /\A(?:$ALLOWED)+\z/u) 1 15 16
24 {
25 1 die
26 "Invalid characters in key \"$key\"! We only allow A-Z, a-z, 0-9, _, dashes and equal signs.";
27 }
28 15 ▶ if ( $key !~ /\A$ALPHAN/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /\A$ALPHAN/u) 1 14 15
29 {
30 1 die qq#Key does not start with an alphanumeric - "$key"!#;
31 }
32 14 ▶ if ( $key !~ /$ALPHAN\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /$ALPHAN\z/u) 1 13 14
33 {
34 1 die qq#Key does not end with an alphanumeric - "$key"!#;
35 }
36  
37 13 return;
38 }
39  
40 has '_keys' => (
41 is => 'ro',
42 lazy => 1,
43 default => sub {
44 7 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
45  
46 7 my @lines = path( $self->manifest_fn )->lines( { chomp => 1 } );
47 7 my $ret = +{};
48  
49 7 foreach my $l (@lines)
50 {
51 15 $self->_is_valid_key($l);
52 12 $ret->{$l} = 1;
53 }
54 4 return $ret;
55 }
56 2 );
57  
58 has '_dh' => (
59 is => 'ro',
60 lazy => 1,
61 default => sub {
62 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
63 3 return path( $self->dir );
64 },
65 2 );
66  
67 sub get_keys
68 {
69 11 ▶ my ($self) = @_;
Subroutine get_keys: called CC=1 SCAR=0.0
Pod documented
70  
71 11 return [ sort { $a cmp $b } keys %{ $self->_keys } ];
72 }
73  
74 sub get_obj
75 {
76 18 ▶ my ( $self, $key ) = @_;
Subroutine get_obj: called CC=2 SCAR=6.9
Pod documented
77  
78 18 ▶ if ( not exists $self->_keys->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_keys->{$key}) 1 17 18
79 {
80 1 die "No such key \"$key\"! Perhaps add it to the manifest.";
81 }
82 17 return Dir::Manifest::Key->new(
83 { key => $key, fh => $self->_dh->child($key) } );
84 }
85  
86 sub fh
87 {
88 16 ▶ my ( $self, $key ) = @_;
Subroutine fh: called CC=1 SCAR=0.0
Pod documented
89  
90 16 return $self->get_obj($key)->fh;
91 }
92  
93 sub text
94 {
95 14 ▶ my ( $self, $key, $opts ) = @_;
Subroutine text: called CC=1 SCAR=0.0
Pod documented
96  
97 14 return Dir::Manifest::Slurp::slurp( $self->fh($key), $opts );
98 }
99  
100 sub texts_dictionary
101 {
102 4 ▶ my ( $self, $args ) = @_;
Subroutine texts_dictionary: called CC=1 SCAR=0.0
Pod documented
103  
104 4 my $opts = $args->{slurp_opts};
105  
106 4 return +{ map { $_ => $self->text( $_, $opts ) } @{ $self->get_keys } };
107 }
108  
109 sub _update_disk_manifest
110 {
111 2 ▶ my $self = shift;
Subroutine _update_disk_manifest: called CC=1 SCAR=0.0
112  
113 2 path( $self->manifest_fn )->spew_raw( map { "$_\n" } @{ $self->get_keys } );
114  
115 2 return;
116 }
117  
118 sub add_key
119 {
120 1 ▶ my ( $self, $args ) = @_;
Subroutine add_key: called CC=2 SCAR=7.0
Pod documented
121  
122 1 my $key = $args->{key};
123 1 my $utf8_val = $args->{utf8_val};
124  
125 1 ▶ if ( exists $self->_keys->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_keys->{$key}) 0 1 1
126 {
127 0 die "Key \"$key\" already exists in the dictionary!";
128 }
129  
130 1 $self->_is_valid_key($key);
131  
132 1 $self->_keys->{$key} = 1;
133  
134 1 $self->_update_disk_manifest;
135 1 $self->fh($key)->spew_utf8($utf8_val);
136  
137 1 return;
138 }
139  
140 sub remove_key
141 {
142 1 ▶ my ( $self, $args ) = @_;
Subroutine remove_key: called CC=2 SCAR=7.1
Pod documented
143  
144 1 my $key = $args->{key};
145  
146 1 ▶ if ( not exists $self->_keys->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_keys->{$key}) 0 1 1
147 {
148 0 die "Key \"$key\" does not exist in the dictionary!";
149 }
150  
151 1 $self->fh($key)->remove;
152 1 delete $self->_keys->{$key};
153 1 $self->_update_disk_manifest;
154  
155 1 return;
156 }
157  
158 2 1;
159  
160 __END__
« Build ↑ Summary blib/lib/Dir/Manifest/Slurp.pm »