| 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 <abw@wardley.org> | ||||||||||||||
| 11 | # | ||||||||||||||
| 12 | #======================================================================== | ||||||||||||||
| 13 | |||||||||||||||
| 14 | package Badger::Filesystem::Base; | ||||||||||||||
| 15 | |||||||||||||||
| 16 | use Badger::Class | ||||||||||||||
| 17 | 70 | 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 | ▶ | }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 70 | our @VDN_FIELDS = qw( volume directory name ); | |||||||||||||
| 26 | 70 | our @VD_FIELDS = qw( volume directory ); | |||||||||||||
| 27 | 70 | our @OPTIONS = qw( encoding codec ); | |||||||||||||
| 28 | |||||||||||||||
| 29 | |||||||||||||||
| 30 | sub init_path { | ||||||||||||||
| 31 | 533 | ▶ | my ($self, $config) = @_; | ||||||||||||
|
Subroutine
init_path: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 32 | 533 | my ($path, $vol, $dir, $name); | |||||||||||||
| 33 | 533 | my $fs = $self->filesystem; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 533 | $self->debug("init_path() ", $self->dump_data($config)) if DEBUG; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 533 | ▶ | if ($config->{ path }) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 38 | # split path into volume, directory, name | ||||||||||||||
| 39 | # set colume, | ||||||||||||||
| 40 | 527 | $path = $self->{ path } = $fs->join_dir( $config->{ path } ); | |||||||||||||
| 41 | 527 | @$self{@VDN_FIELDS} = $fs->split_path($path); | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | elsif ($self->{ name } = $config->{ name }) { | ||||||||||||||
| 44 | 6 | ▶ | @$self{@VD_FIELDS} = ($vol, $dir) = map { defined($_) ? $_ : '' } @$config{ @VD_FIELDS }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 6 | $self->{ path } = $fs->join_path($vol, $dir, $self->{ name }); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | else { | ||||||||||||||
| 48 | 0 | $self->error_msg( missing => 'path or name' ); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 533 | return $self; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | sub init_options { | ||||||||||||||
| 55 | 559 | ▶ | my ($self, $config) = @_; | ||||||||||||
|
Subroutine
init_options: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 56 | 559 | my $opts = $self->{ options } = { }; | |||||||||||||
| 57 | 559 | my $method; | |||||||||||||
| 58 | |||||||||||||||
| 59 | 559 | foreach my $name (@OPTIONS) { | |||||||||||||
| 60 | 1118 | ▶ | if ($config->{ $name }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 6 | ▶ | $method = $self->can($name) | ||||||||||||
|
Condition50%1/2
$self->can($name) || (return $self->error_msg('no_option_method', $name))
MC/DC0%0/1
$self->can($name) || (return $self->error_msg('no_option_method', $name))
$self->can($name)
Truth table50%1/2
A
A= $self->can($name)
| |||||||||||||||
| 62 | || return $self->error_msg( no_option_method => $name ); | ||||||||||||||
| 63 | 6 | $method->( $self, $config->{ $name } ); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 559 | return $self; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | sub codec { | ||||||||||||||
| 71 | 9 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
codec: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 72 | 9 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 8 | require Badger::Codecs; | |||||||||||||
| 74 | 8 | $self->{ options }->{ codec } = Badger::Codecs->codec(@_); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | 9 | return $self->{ options }->{ codec }; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | sub encoding { | ||||||||||||||
| 80 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
encoding: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 81 | 2 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 2 | my $layer = shift; | |||||||||||||
| 83 | # be generous in what you accept... | ||||||||||||||
| 84 | 2 | ▶ | $layer = ":$layer" unless $layer =~ /^:/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 2 | $self->{ options }->{ encoding } = $layer; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | 2 | return $self->{ options }->{ encoding }; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | |||||||||||||||
| 91 | 70 | 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<Badger::Filesystem::Base> is a base class module that defines some common | ||||||||||||||
| 106 | functionality shared by L<Badger::Filesystem> and L<Badger::Filesystem::Path> | ||||||||||||||
| 107 | (which itself is the base class for L<Badger::Filesystem::Directory> and | ||||||||||||||
| 108 | L<Badger::Filesystem::File>. | ||||||||||||||
| 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<encoding> and C<codec> 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<data()> method. The codec should be specified | ||||||||||||||
| 141 | by name, using any of the names that L<Badger::Codecs> 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<Badger::Codecs> for further information on codecs. | ||||||||||||||
| 158 | |||||||||||||||
| 159 | As with L<encoding()>, 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<http://wardley.org/> | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =head1 COPYRIGHT | ||||||||||||||
| 170 | |||||||||||||||
| 171 | Copyright (C) 2009 Andy Wardley. All rights reserved. | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =head1 SEE ALSO | ||||||||||||||
| 174 | |||||||||||||||
| 175 | L<Badger::Filesystem>, | ||||||||||||||
| 176 | L<Badger::Filesystem::Path>, | ||||||||||||||
| 177 | L<Badger::Filesystem::Directory>, | ||||||||||||||
| 178 | L<Badger::Filesystem::File>. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =cut | ||||||||||||||
| 181 | |||||||||||||||