| 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 |  |  |  |  | 500 | 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 |  | 405 | }; | 
|  | 70 |  |  |  |  | 113 |  | 
| 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 | 533 |  |  | 533 | 1 | 617 | my ($self, $config) = @_; | 
| 32 | 533 |  |  |  |  | 558 | my ($path, $vol, $dir, $name); | 
| 33 | 533 |  |  |  |  | 925 | my $fs = $self->filesystem; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 533 |  |  |  |  | 543 | $self->debug("init_path() ", $self->dump_data($config)) if DEBUG; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 533 | 100 |  |  |  | 752 | if ($config->{ path }) { | 
|  |  | 50 |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # split path into volume, directory, name | 
| 39 |  |  |  |  |  |  | # set colume, | 
| 40 | 527 |  |  |  |  | 1017 | $path = $self->{ path } = $fs->join_dir( $config->{ path } ); | 
| 41 | 527 |  |  |  |  | 927 | @$self{@VDN_FIELDS} = $fs->split_path($path); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | elsif ($self->{ name } = $config->{ name }) { | 
| 44 | 6 | 100 |  |  |  | 14 | @$self{@VD_FIELDS} = ($vol, $dir) = map { defined($_) ? $_ : '' } @$config{ @VD_FIELDS }; | 
|  | 12 |  |  |  |  | 40 |  | 
| 45 | 6 |  |  |  |  | 18 | $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 | 533 |  |  |  |  | 878 | return $self; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub init_options { | 
| 55 | 559 |  |  | 559 | 1 | 680 | my ($self, $config) = @_; | 
| 56 | 559 |  |  |  |  | 1136 | my $opts = $self->{ options } = { }; | 
| 57 | 559 |  |  |  |  | 719 | my $method; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 559 |  |  |  |  | 845 | foreach my $name (@OPTIONS) { | 
| 60 | 1118 | 100 |  |  |  | 1781 | if ($config->{ $name }) { | 
| 61 | 6 |  | 50 |  |  | 43 | $method = $self->can($name) | 
| 62 |  |  |  |  |  |  | || return $self->error_msg( no_option_method => $name ); | 
| 63 | 6 |  |  |  |  | 17 | $method->( $self, $config->{ $name } ); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 559 |  |  |  |  | 778 | return $self; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub codec { | 
| 71 | 9 |  |  | 9 | 1 | 24 | my $self = shift; | 
| 72 | 9 | 100 |  |  |  | 18 | if (@_) { | 
| 73 | 8 |  |  |  |  | 1093 | require Badger::Codecs; | 
| 74 | 8 |  |  |  |  | 42 | $self->{ options }->{ codec } = Badger::Codecs->codec(@_); | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 9 |  |  |  |  | 26 | return $self->{ options }->{ codec }; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub encoding { | 
| 80 | 2 |  |  | 2 | 1 | 4 | my $self = shift; | 
| 81 | 2 | 50 |  |  |  | 9 | if (@_) { | 
| 82 | 2 |  |  |  |  | 4 | my $layer = shift; | 
| 83 |  |  |  |  |  |  | # be generous in what you accept... | 
| 84 | 2 | 50 |  |  |  | 13 | $layer = ":$layer" unless $layer =~ /^:/; | 
| 85 | 2 |  |  |  |  | 8 | $self->{ options }->{ encoding } = $layer; | 
| 86 |  |  |  |  |  |  | } | 
| 87 | 2 |  |  |  |  | 5 | 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 |  |  |  |  |  |  |  |