File Coverage

blib/lib/App/Open/Backend/YAML.pm
Criterion Covered Total %
statement 31 31 100.0
branch 6 8 75.0
condition 9 17 52.9
subroutine 8 8 100.0
pod 5 5 100.0
total 59 69 85.5


line stmt bran cond sub pod time code
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 (),
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 3     3   41699 use strict;
  3         5  
  3         113  
21 3     3   16 use warnings;
  3         6  
  3         75  
22              
23 3     3   941 use YAML::Syck;
  3         2584  
  3         1810  
24             $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             sub new {
77 10     10 1 4630 my ( $class, $def_file ) = @_;
78              
79 10   100     56 $def_file ||= [];
80              
81 10 50 33     98 die /BACKEND_CONFIG_ERROR/ if ($def_file && (!ref($def_file) || !ref($def_file) eq 'ARRAY'));
      33        
82              
83 10   66     71 my $self = bless {
84             def_file => ($def_file->[0] || "$ENV{HOME}/.mimeyaml")
85             }, $class;
86              
87 10         77 $self->load_definitions;
88              
89 6         25 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             sub load_definitions {
104 10     10 1 19 my $self = shift;
105              
106 10         20 my $config;
107              
108 10         15 eval { $config = LoadFile( $self->def_file ) };
  10         31  
109              
110             #
111             # so you think you're tough, eh?
112             #
113             # let's see if you can pass... A SYNTAX CHECK!
114             #
115 10 100       3079 if ($@) {
116 2         28 die "BACKEND_CONFIG_ERROR";
117             }
118             else {
119 8 100 66     87 if ( $config and ref($config) eq 'HASH' ) {
120 6         23 foreach my $value ( values %$config ) {
121 18 50 33     96 die "BACKEND_CONFIG_ERROR" unless ( $value and !ref($value) );
122             }
123             }
124             else {
125 2         32 die "BACKEND_CONFIG_ERROR";
126             }
127             }
128              
129 6         22 $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             sub lookup_file {
139 14     14 1 67 my ( $self, $extension ) = @_;
140              
141 14         33 $extension =~ s/^\.//g;
142              
143 14         61 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 2     2 1 15 sub lookup_url { $_[0]->lookup_file($_[1].":") }
155              
156             =item def_file
157              
158             Returns the filename where the defintions used are kept.
159              
160             =cut
161              
162 11     11 1 986 sub def_file { $_[0]->{def_file} }
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 if you're feeling
174             kind. Report them to CPAN RT if you'd prefer they never get seen.
175              
176             =cut
177              
178             1;