| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Filesystem::File | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # OO representation of a file in a filesystem. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Filesystem::File; | ||
| 14 | |||
| 15 | use Badger::Class | ||
| 16 | version => 0.01, | ||
| 17 | base => 'Badger::Filesystem::Path', | ||
| 18 | debug => 0, | ||
| 19 | dumps => 'path volume directory name stats', | ||
| 20 | import => 'class', | ||
| 21 | constants => 'ARRAY BLANK', | ||
| 22 | constant => { | ||
| 23 | type => 'File', | ||
| 24 | is_file => 1, | ||
| 25 | }; | ||
| 26 | |||
| 27 | |||
| 28 | # aliases | ||
| 29 | *base = \&directory; | ||
| 30 | *copy = \©_to; | ||
| 31 | *move = \&move_to; | ||
| 32 | |||
| 33 | |||
| 34 | sub init { | ||
| 35 | my ($self, $config) = @_; | ||
| 36 | $self->init_path($config); | ||
| 37 | $self->init_options($config); | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub directory { | ||
| 42 | my $self = shift; | ||
| 43 | return @_ | ||
| 44 | ? $self->filesystem->directory( $self->relative(@_) ) | ||
| 45 | : $self->parent; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub file { | ||
| 49 | my $self = shift; | ||
| 50 | return @_ | ||
| 51 | ? $self->filesystem->file( $self->relative(@_) ) | ||
| 52 | : $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub exists { | ||
| 56 | my $self = shift; | ||
| 57 | # cache the stats returned in case we want them later | ||
| 58 | return ($self->{ stats } = $self->filesystem->file_exists($self->{ path })); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub create { | ||
| 62 | my $self = shift; | ||
| 63 | $self->filesystem->create_file($self->{ path }, @_); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub touch { | ||
| 67 | my $self = shift; | ||
| 68 | $self->filesystem->touch_file($self->{ path }); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub open { | ||
| 72 | my $self = shift; | ||
| 73 | $self->filesystem->open_file($self->{ path }, @_, $self->{ options }); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub read { | ||
| 77 | my $self = shift; | ||
| 78 | $self->filesystem->read_file($self->{ path }, @_, $self->{ options }); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub write { | ||
| 82 | my $self = shift; | ||
| 83 | $self->filesystem->write_file($self->{ path }, @_, $self->{ options }); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub append { | ||
| 87 | my $self = shift; | ||
| 88 | $self->filesystem->append_file($self->{ path }, @_, $self->{ options }); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub copy_to { | ||
| 92 | my $self = shift; | ||
| 93 | $self->filesystem->copy_file($self->{ path }, @_); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub copy_from { | ||
| 97 | my $self = shift; | ||
| 98 | $self->filesystem->copy_file(shift, $self->{ path }, @_); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub move_to { | ||
| 102 | my $self = shift; | ||
| 103 | $self->filesystem->move_file($self->{ path }, @_); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub move_from { | ||
| 107 | my $self = shift; | ||
| 108 | $self->filesystem->move_file(shift, $self->{ path }, @_); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub print { | ||
| 112 | my $self = shift; | ||
| 113 | $self->write( join(BLANK, @_) ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub delete { | ||
| 117 | my $self = shift; | ||
| 118 | $self->filesystem->delete_file($self->{ path }, @_); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub text { | ||
| 122 | my $self = shift; | ||
| 123 | my $text = $self->read(@_, $self->{ options }); | ||
| 124 | return $text; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub data { | ||
| 128 | my $self = shift; | ||
| 129 | my $codec = $self->{ options }->{ codec }; | ||
| 130 | my $data; | ||
| 131 | |||
| 132 | $self->debug("filesystem codec: $codec") if DEBUG; | ||
| 133 | |||
| 134 | if (@_) { | ||
| 135 | $data = @_ == 1 ? shift : [@_]; | ||
| 136 | $data = $codec->encode($data) if $codec; | ||
| 137 | return $self->write($data); | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | $data = $self->read; | ||
| 141 | $data = $codec->decode($data) if $codec; | ||
| 142 | return $data; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub accept { | ||
| 147 | $_[1]->visit_file($_[0]); | ||
| 148 | } | ||
| 149 | |||
| 150 | class->methods( | ||
| 151 | map { | ||
| 152 | my $item = $_; # lexical copy for closure | ||
| 153 | $item => sub { | ||
| 154 | my $self = shift; | ||
| 155 | # ...and strict in what you provide | ||
| 156 | $self->encoding(':' . $item); | ||
| 157 | return $self; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | qw( raw utf8 crlf bytes ) | ||
| 161 | ); | ||
| 162 | |||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | __END__ |