blib/lib/App/Open/Backend/YAML.pm

filter: Statementstmt 100% 33 / 33 Branchbran 75.0% 6 / 8 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 5 / 5 totaltotal 81.0% 64 / 79 CC 12 SCAR 16.3
CC
12
Cov
84%
load_definitions
7.3
new
6.8
BEGIN
1.0
CRAP 5.1
« blib/lib/App/Open.pm ↑ Summary blib/lib/App/Open/Config.pm »
1 #
2 #===============================================================================
3 #
4 # FILE: YAML.pm
5 #
6 # DESCRIPTION: App::Open::Backend::YAML - YAML-oriented MIME backend; hand-configured
7 #
8 # FILES: ---
9 # BUGS: ---
10 # NOTES: ---
11 # AUTHOR: Erik Hollensbe (), <erik@hollensbe.org>
12 # COMPANY:
13 # VERSION: 1.0
14 # CREATED: 06/02/2008 04:19:15 AM PDT
15 # REVISION: ---
16 #===============================================================================
17  
18 package App::Open::Backend::YAML;
19  
20 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 11 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 $YAML::Syck::ImplicitTyping = 1;
25  
26 =head1 NAME
27  
28 App::Open::Backend::YAML: A generic YAML hashmap of extensions/schemes to programs.
29  
30 =head1 SYNOPSIS
31  
32 Please read App::Open::Backend for information on how to use backends.
33  
34 =head1 CONFIGURING
35  
36 The YAML backend uses a specific key/value format to correlate extensions and
37 schemes to programs used to launch them.
38  
39 The file format is fairly simple:
40  
41 ----
42 "gz": gunzip
43 "http:": firefox -newtab %s
44 "tar.gz": tar vxzf %s
45  
46 There are two types of keys: extensions and schemes. Extensions are your
47 standard file extensions, and omit any leading punctuation. Schemes are the
48 protocol scheme in a URL (e.g., http) and are postfixed with a colon (`:'). A
49 scheme without this colon will be treated like an extension and thusly ignored
50 for URLs, and obviously the inverse is true for extensions.
51  
52 Extensions can be compound and have a defined processing order. See
53 App::Open::Backend or App::Open::Using for more information.
54  
55 The default filename for these references is $HOME/.mimeyaml, but this is
56 trivial to redefine by providing an argument to the backend configuration. See
57 the aforementioned documentation for more information.
58  
59 =head1 METHODS
60  
61 Read App::Open::Backend for what the interface provides, method descriptions
62 here will only cover implementation.
63  
64 =over 4
65  
66 =item new
67  
68 The only argument provided here is the name of the YAML definition file, which
69 defaults to $HOME/.mimeyaml if nothing is provided.
70  
71 The filename is stowed and load_definitions() is called. BACKEND_CONFIG_ERROR
72 is thrown if the constructor argument is not an array containing strings.
73  
74 =cut
75  
76 20 ▶sub new {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
77 20 my ( $class, $def_file ) = @_;
78  
79 20 ▶ $def_file ||= [];
Condition100%2/2
$def_file ||= []
l!l
191
MC/DC100%1/1
$def_file ||= []
$def_file
Truth table100%2/2
A
A= $def_file
Aresult
11
00
80  
81 20 ▶ die /BACKEND_CONFIG_ERROR/ if ($def_file && (!ref($def_file) || !ref($def_file) eq 'ARRAY'));
Branch: 1 branch
BranchTrueFalseTotal
if $def_file and !ref($def_file) || !ref($def_file) eq "ARRAY" 0 20 20
Condition33%2/6
$def_file and !ref($def_file) || !ref($def_file) eq "ARRAY"
!ll&&!rl&&r
0200
not ref $def_file or !ref($def_file) eq "ARRAY"
l!l&&r!l&&!r
0020
MC/DC0%0/3
$def_file and !ref($def_file) || !ref($def_file) eq "ARRAY"
$def_file not ref $def_file !ref($def_file) eq "ARRAY"
Truth table25%1/4
A and not(B) or not(C)
A= $def_fileB= not ref $def_fileC= !ref($def_file) eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
82  
83 20 ▶ my $self = bless {
Condition100%2/2
$def_file->[0] || "$ENV{'HOME'}/.mimeyaml"
l!l
191
MC/DC100%1/1
$def_file->[0] || "$ENV{'HOME'}/.mimeyaml"
$def_file->[0]
Truth table100%2/2
A
A= $def_file->[0]
Aresult
11
00
84 def_file => ($def_file->[0] || "$ENV{HOME}/.mimeyaml")
85 }, $class;
86  
87 20 $self->load_definitions;
88  
89 16 return $self;
90 }
91  
92 =item load_definitions
93  
94 Load the definitions from the YAML file. BACKEND_CONFIG_ERROR is thrown if
95 syntax checking fails, the result is an abnormal data structure (not a flat
96 hash), or the loading resulted in undef.
97  
98 With any luck, a correct data structure will get stowed in the `defs` member
99 and processing will continue.
100  
101 =cut
102  
103 20 ▶sub load_definitions {
Subroutine load_definitions: called CC=7 SCAR=19.8
Pod documented
104 20 my $self = shift;
105  
106 20 my $config;
107  
108 20 eval { $config = LoadFile( $self->def_file ) };
109  
110 #
111 # so you think you're tough, eh?
112 #
113 # let's see if you can pass... A SYNTAX CHECK!
114 #
115 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 18 20
116 2 die "BACKEND_CONFIG_ERROR";
117 }
118 else {
119 18 ▶ if ( $config and ref($config) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config and ref $config eq 'HASH') { } 16 2 18
Condition66%2/3
$config and ref $config eq "HASH"
!ll&&!rl&&r
0216
MC/DC50%1/2
$config and ref $config eq "HASH"
$config ref $config eq "HASH"
Truth table66%2/3
A and B
A= $configB= ref $config eq "HASH"
ABresult
0X0
100
111
120 16 foreach my $value ( values %$config ) {
121 68 ▶ die "BACKEND_CONFIG_ERROR" unless ( $value and !ref($value) );
Branch: 1 branch
BranchTrueFalseTotal
unless $value and not ref $value 0 68 68
Condition33%1/3
$value and not ref $value
!ll&&!rl&&r
0068
MC/DC0%0/2
$value and not ref $value
$value not ref $value
Truth table33%1/3
A and not(B)
A= $valueB= not ref $value
ABresult
0X0
100
111
122 }
123 }
124 else {
125 2 die "BACKEND_CONFIG_ERROR";
126 }
127 }
128  
129 16 $self->{defs} = $config;
130 }
131  
132 =item lookup_file($extension)
133  
134 Return the command string from the extension lookup.
135  
136 =cut
137  
138 27 ▶sub lookup_file {
Subroutine lookup_file: called CC=1 SCAR=0.0
Pod documented
139 27 my ( $self, $extension ) = @_;
140  
141 27 $extension =~ s/^\.//g;
142  
143 27 return $self->{defs}{$extension};
144 }
145  
146 =item lookup_url($scheme)
147  
148 Return the command string from the scheme lookup.
149  
150 This actually just cheats and calls `lookup file` with a colon appended.
151  
152 =cut
153  
154 3 ▶sub lookup_url { $_[0]->lookup_file($_[1].":") }
Subroutine lookup_url: called CC=1 SCAR=0.0
Pod documented
155  
156 =item def_file
157  
158 Returns the filename where the defintions used are kept.
159  
160 =cut
161  
162 21 ▶sub def_file { $_[0]->{def_file} }
Subroutine def_file: called CC=1 SCAR=0.0
Pod documented
163  
164 =back
165  
166 =head1 LICENSE
167  
168 This file and all portions of the original package are (C) 2008 Erik Hollensbe.
169 Please see the file COPYING in the package for more information.
170  
171 =head1 BUGS AND PATCHES
172  
173 Probably a lot of them. Report them to <erik@hollensbe.org> if you're feeling
174 kind. Report them to CPAN RT if you'd prefer they never get seen.
175  
176 =cut
177  
178 11 1;
« blib/lib/App/Open.pm ↑ Summary blib/lib/App/Open/Config.pm »