File Coverage

lib/Badger/Filesystem/Base.pm
Criterion Covered Total %
statement 33 34 97.0
branch 11 14 78.5
condition 1 2 50.0
subroutine 5 5 100.0
pod 4 4 100.0
total 54 59 91.5


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Filesystem::Base
4             #
5             # DESCRIPTION
6             # Base class for Badger::Filesystem modules implementing some common
7             # functionality.
8             #
9             # AUTHOR
10             # Andy Wardley
11             #
12             #========================================================================
13              
14             package Badger::Filesystem::Base;
15              
16             use Badger::Class
17 70         529 version => 0.01,
18             base => 'Badger::Base',
19             debug => 0,
20             import => 'class',
21             messages => {
22             no_option_method => 'No method defined to handle %s option',
23 70     70   456 };
  70         123  
24              
25             our @VDN_FIELDS = qw( volume directory name );
26             our @VD_FIELDS = qw( volume directory );
27             our @OPTIONS = qw( encoding codec );
28              
29              
30             sub init_path {
31 538     538 1 807 my ($self, $config) = @_;
32 538         659 my ($path, $vol, $dir, $name);
33 538         1086 my $fs = $self->filesystem;
34              
35 538         667 $self->debug("init_path() ", $self->dump_data($config)) if DEBUG;
36              
37 538 100       939 if ($config->{ path }) {
    50          
38             # split path into volume, directory, name
39             # set colume,
40 532         1136 $path = $self->{ path } = $fs->join_dir( $config->{ path } );
41 532         1285 @$self{@VDN_FIELDS} = $fs->split_path($path);
42             }
43             elsif ($self->{ name } = $config->{ name }) {
44 6 100       21 @$self{@VD_FIELDS} = ($vol, $dir) = map { defined($_) ? $_ : '' } @$config{ @VD_FIELDS };
  12         38  
45 6         24 $self->{ path } = $fs->join_path($vol, $dir, $self->{ name });
46             }
47             else {
48 0         0 $self->error_msg( missing => 'path or name' );
49             }
50              
51 538         1068 return $self;
52             }
53              
54             sub init_options {
55 564     564 1 852 my ($self, $config) = @_;
56 564         1382 my $opts = $self->{ options } = { };
57 564         855 my $method;
58              
59 564         946 foreach my $name (@OPTIONS) {
60 1128 100       2138 if ($config->{ $name }) {
61 6   50     40 $method = $self->can($name)
62             || return $self->error_msg( no_option_method => $name );
63 6         15 $method->( $self, $config->{ $name } );
64             }
65             }
66              
67 564         861 return $self;
68             }
69              
70             sub codec {
71 9     9 1 22 my $self = shift;
72 9 100       17 if (@_) {
73 8         923 require Badger::Codecs;
74 8         33 $self->{ options }->{ codec } = Badger::Codecs->codec(@_);
75             }
76 9         24 return $self->{ options }->{ codec };
77             }
78              
79             sub encoding {
80 2     2 1 4 my $self = shift;
81 2 50       6 if (@_) {
82 2         5 my $layer = shift;
83             # be generous in what you accept...
84 2 50       10 $layer = ":$layer" unless $layer =~ /^:/;
85 2         6 $self->{ options }->{ encoding } = $layer;
86             }
87 2         4 return $self->{ options }->{ encoding };
88             }
89              
90              
91             1;
92              
93             =head1 NAME
94              
95             Badger::Filesystem::Base - common functionality for Badger::Filesystem modules
96              
97             =head1 SYNOPSIS
98              
99             package Badger::Filesystem::SomeOtherModule;
100             use base 'Badger::Filesystem::Base'
101             # now this module inherits the base class functionality
102              
103             =head1 DESCRIPTION
104              
105             C is a base class module that defines some common
106             functionality shared by L and L
107             (which itself is the base class for L and
108             L.
109              
110             =head1 METHODS
111              
112             =head2 init_path(\%config)
113              
114             Initialisation method which examines the filesystem path specified as a
115             parameter and splits it into volume, directory and name.
116              
117             =head2 init_options(\%config)
118              
119             Initialisation method which handles the C and C options.
120              
121             =head2 encoding($enc)
122              
123             This method can be used to get or set the default encoding for a file.
124              
125             $file->encoding(':utf8');
126              
127             The encoding will affect all operations that read data from, or write data
128             to the file.
129              
130             The method can also be used to get or set the default encoding for a
131             directory or filesystem. In this case the option specifies the default
132             encoding for file contained therein.
133              
134             $directory->encoding(':utf8');
135             $file = $directory->file('foo.txt'); # has :utf8 encoding set
136              
137             =head2 codec()
138              
139             This method can be used to get or set the codec used to serialise data to
140             and from a file via the L method. The codec should be specified
141             by name, using any of the names that L recognises or can
142             load.
143              
144             $file->codec('storable');
145            
146             # first save the data to file
147             $file->data($some_data_to_save);
148            
149             # later... load the data back out
150             my $data = $file->data;
151              
152             You can use chained codec specifications if you want to pass the data
153             through more than one codec.
154              
155             $file->code('storable+base64');
156              
157             See L for further information on codecs.
158              
159             As with L, this method can also be used to get or set the default
160             codec for a directory or filesystem.
161              
162             $directory->codec('json');
163             $file = $directory->file('foo.json'); # has json codec set
164              
165             =head1 AUTHOR
166              
167             Andy Wardley L
168              
169             =head1 COPYRIGHT
170              
171             Copyright (C) 2009 Andy Wardley. All rights reserved.
172              
173             =head1 SEE ALSO
174              
175             L,
176             L,
177             L,
178             L.
179              
180             =cut
181