KiokuDB/Util.pm

filter: Statementstmt 32.3% 33 / 102 Branchbran 3.5% 1 / 28 Conditioncond 9.0% 1 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 45.0% 9 / 20 Podpod 44.4% 4 / 9 totaltotal 27.1% 48 / 177 CC 21 SCAR 26.0
CC
21
Cov
25%
load_yaml_files
30.0
dsn_to_backend
24.1
deprecate
20.0
CRAP 13.4
« KiokuDB/Role/WithDigest.pm ↑ Summary KiokuDB/Test/Fixture/Concurrency.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Util;
4  
5 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 21 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 use Sub::Exporter -setup => {
16 exports => [qw(set weak_set dsn_to_backend import_yaml deprecate)],
17 21 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub weak_set {
20 0 ▶ require KiokuDB::Set::Transient;
Subroutine weak_set: not called CC=1 SCAR=6.9
Pod documented
21 0 KiokuDB::Set::Transient->new( set => Set::Object::Weak->new(@_) )
22 }
23  
24 sub set {
25 0 ▶ require KiokuDB::Set::Transient;
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
26 0 KiokuDB::Set::Transient->new( set => Set::Object->new(@_) );
27 }
28  
29 21 my %monikers = (
30 "hash" => "Hash",
31 "bdb" => "BDB",
32 "bdb-gin" => "BDB::GIN",
33 "dbi" => "DBI",
34 "jspon" => "JSPON",
35 "files" => "Files",
36 "couchdb" => "CouchDB",
37 "mongodb" => "MongoDB",
38 );
39  
40 sub _try_json {
41 0 ▶ my $json = shift;
Subroutine _try_json: not called CC=1 SCAR=6.9
42  
43 0 require JSON;
44 0 JSON->new->decode($json);
45 }
46  
47 sub dsn_to_backend {
48 48 ▶ my ( $dsn, @args ) = @_;
Subroutine dsn_to_backend: called CC=7 SCAR=31.8
Pod documented
49  
50 48 ▶ if ( my ( $moniker, $rest ) = ( $dsn =~ /^([\w-]+)(?::(.*))?$/ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $dsn =~ /^([\w-]+)(?::(.*))?$/) { } 48 0 48
elsif (my $args = _try_json($dsn)) { } 0 0 0
51 48 ▶ $moniker = $monikers{$moniker} || $moniker;
Condition33%1/3
$monikers{$moniker} || $moniker
l!l&&r!l&&!r
4800
MC/DC0%0/2
$monikers{$moniker} || $moniker
$monikers{$moniker} $moniker
Truth table33%1/3
A || B
A= $monikers{$moniker}B= $moniker
ABresult
1X1
011
000
52 48 my $class = "KiokuDB::Backend::$moniker";
53  
54 48 Class::MOP::load_class($class);
55 48 return $class->new_from_dsn($rest, @args);
56 } elsif ( my $args = _try_json($dsn) ) {
57 0 my $dsn;
58  
59 0 ▶ if ( ref $args eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "ARRAY") 0 0 0
60 0 ( $dsn, $args ) = @$args;
61 }
62  
63 0 ▶ if ( ref $args eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 0 0 0
64 0 ▶ $dsn ||= delete $args->{dsn};
Condition0%0/3
$dsn ||= delete $args->{"dsn"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dsn ||= delete $args->{"dsn"}
$dsn delete $args->{"dsn"}
Truth table0%0/3
A ||= B
A= $dsnB= delete $args->{"dsn"}
ABresult
1X1
011
000
65 0 return dsn_to_backend($dsn, %$args, @args);
66 }
67 }
68  
69 0 croak "Malformed DSN: $dsn";
70 }
71  
72 sub load_config {
73 0 ▶ my ( $base ) = @_;
Subroutine load_config: not called CC=2 SCAR=17.9
Pod undocumented
74  
75 0 my $config_file;
76 0 ▶ if ( $base =~ /\.yml$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ /\.yml$/) { } 0 0 0
77 0 $config_file = $base;
78 } else {
79 0 $config_file = dir($base)->file("kiokudb.yml");
80 0 $config_file->openr;
81 }
82  
83  
84 0 require MooseX::YAML;
85 0 MooseX::YAML::LoadFile($config_file);
86 }
87  
88 sub config_to_backend {
89 0 ▶ my ( $config, %args ) = @_;
Subroutine config_to_backend: not called CC=3 SCAR=24.8
Pod undocumented
90  
91 0 my $base = delete($args{base});
92  
93 0 my $backend = $config->{backend};
94  
95 0 ▶ return $backend if blessed($backend);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($backend) 0 0 0
96  
97 0 my $backend_class = $backend->{class};
98 0 Class::MOP::load_class($backend_class);
99  
100 0 ▶ return $backend_class->new_from_dsn_params(
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 0 0 0
101 ( defined($base) ? ( dir => $base->subdir("data") ) : () ),
102 %$backend,
103 %args,
104 );
105 }
106  
107 sub import_yaml {
108 0 ▶ my ( $kiokudb, @src ) = @_;
Subroutine import_yaml: not called CC=1 SCAR=6.9
Pod documented
109  
110 0 my @objects = load_yaml_files( find_yaml_files(@src) );
111  
112 $kiokudb->txn_do(sub {
113 0 ▶ my $scope = $kiokudb->new_scope;
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 $kiokudb->insert(@objects);
115 0 });
116 }
117  
118 sub find_yaml_files {
119 0 ▶ my ( @src ) = @_;
Subroutine find_yaml_files: not called CC=3 SCAR=24.8
Pod undocumented
120  
121 0 my @files;
122  
123 0 foreach my $src ( @src ) {
124 0 ▶ if ( -d $src ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $src) { } 0 0 0
125 dir($src)->recurse( callback => sub {
126 0 ▶ my $file = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
127  
128 0 ▶ if ( -f $file && $file->basename =~ /\.yml$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and $file->basename =~ /\.yml$/) 0 0 0
Condition0%0/3
-f $file and $file->basename =~ /\.yml$/
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and $file->basename =~ /\.yml$/
-f $file $file->basename =~ /\.yml$/
Truth table0%0/3
A and B
A= -f $fileB= $file->basename =~ /\.yml$/
ABresult
0X0
100
111
129 0 push @files, $file;
130 }
131 0 });
132 } else {
133 0 push @files, $src;
134 }
135 }
136  
137 0 return @files;
138 }
139  
140 sub load_yaml_files {
141 0 ▶ my ( @files ) = @_;
Subroutine load_yaml_files: not called CC=5 SCAR=34.0
Pod undocumented
142  
143 0 my @objects;
144  
145 0 require MooseX::YAML;
146  
147 0 foreach my $file ( @files ) {
148 0 my @data = MooseX::YAML::LoadFile($file);
149  
150 0 ▶ if ( @data == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1) 0 0 0
151 0 ▶ unless ( blessed $data[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($data[0])) 0 0 0
152 0 ▶ if ( ref $data[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data[0] eq 'ARRAY') { } 0 0 0
153 0 @data = @{ $data[0] };
154 } else {
155 0 @data = %{ $data[0] }; # with IDs
156 }
157 }
158 }
159  
160 0 push @objects, @data;
161 }
162  
163 0 return @objects;
164 }
165  
166 21 my %seen_deprecation;
167  
168 21 ▶use constant HARNESS_ACTIVE => not not $ENV{HARNESS_ACTIVE};
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 sub deprecate ($$) {
171 0 ▶ if ( HARNESS_ACTIVE ) {
Subroutine deprecate: not called CC=4 SCAR=30.0
Pod undocumented
172 0 my ( $version, $reason ) = @_;
173  
174 # parts stolen from Devel::Deprecate, but we're doing version based
175 # deprecation, not date based deprecation
176  
177 0 require KiokuDB;
178  
179 0 ▶ if ( $KiokuDB::VERSION >= $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($KiokuDB::VERSION >= $version) 0 0 0
180 0 my ( $package, $filename, $line ) = caller(1);
181 0 my ( undef, undef, undef, $subroutine ) = caller(2);
182  
183 0 ▶ return if $seen_deprecation{"${filename}:$line"}++; # no need to warn more than once
Branch: 1 branch
BranchTrueFalseTotal
if $seen_deprecation{"${filename}:$line"}++ 0 0 0
184  
185 0 ▶ $subroutine ||= 'n/a';
Condition0%0/2
$subroutine ||= "n/a"
l!l
00
MC/DC0%0/1
$subroutine ||= "n/a"
$subroutine
Truth table0%0/2
A
A= $subroutine
Aresult
11
00
186 0 my $padding = ' ' x 18;
187 0 $reason =~ s/\n/\n#$padding/g;
188  
189 0 Carp::cluck(<<"END");
190 # DEPRECATION WARNING
191 #
192 # Package: $package
193 # File: $filename
194 # Line: $line
195 # Subroutine: $subroutine
196 #
197 # Reason: $reason
198 END
199 }
200 }
201 }
202  
203 21 __PACKAGE__
204  
205 __END__
206  
207 =pod
208  
209 =head1 NAME
210  
211 KiokuDB::Util - Utility functions for working with KiokuDB
212  
213 =head1 SYNOPSIS
214  
215 use KiokuDB::Util qw(set weak_set);
216  
217 my $set = set(@objects); # create a transient set
218  
219 my $weak = weak_set(@objects); # to avoid circular refs
220  
221 =head1 DESCRIPTION
222  
223 This module provides various helper functions for working with L<KiokuDB>.
224  
225 =head1 EXPORTS
226  
227 =over 4
228  
229 =item dsn_to_backend $dsn, %args
230  
231 Tries to parse C<$dsn>, load the backend and invoke C<new> on it.
232  
233 Used by L<KiokuDB/connect> and the various command line interfaces.
234  
235 =item set
236  
237 =item weak_set
238  
239 Instantiate a L<Set::Object> or L<Set::Object::Weak> from the arguments, and
240 then creates a L<KiokuDB::Set::Transient> with the result.
241  
242 =item import_yaml $kiokudb, @files_or_dirs
243  
244 Loads YAML files with L<MooseX::YAML> (if given a directory it will be searched
245 recursively for files with a C<.yml> extension are) into the specified KiokuDB
246 directory in a single transaction.
247  
248 The YAML files can contain multiple documents, with each document treated as an
249 object. If the YAML file contains a single non blessed array or hash then that
250 structure will be dereferenced as part of the arguments to C<insert>.
251  
252 Here is an example of an array of objects, and a custom tag alias to ease
253 authoring of the YAML file:
254  
255 %YAML 1.1
256 %TAG ! !MyFoo::
257 ---
258 - !User
259 id: foo
260 real_name: Foo Bar
261 email: foo@myfoo.com
262 password: '{cleartext}test123'
263  
264 You can use a hash to specify custom IDs:
265  
266 %YAML 1.1
267 ---
268 the_id: !Some::Class
269 attr: moose
270  
271 =back
272  
273 =cut
274  
275  
« KiokuDB/Role/WithDigest.pm ↑ Summary KiokuDB/Test/Fixture/Concurrency.pm »