| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CatalystX::CRUD::Object::File; | 
| 2 | 5 |  |  | 5 |  | 4371 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 160 |  | 
| 3 | 5 |  |  | 5 |  | 41 | use warnings; | 
|  | 5 |  |  |  |  | 43 |  | 
|  | 5 |  |  |  |  | 146 |  | 
| 4 | 5 |  |  | 5 |  | 26 | use base qw( CatalystX::CRUD::Object ); | 
|  | 5 |  |  |  |  | 24 |  | 
|  | 5 |  |  |  |  | 1876 |  | 
| 5 | 5 |  |  | 5 |  | 42 | use Path::Class::File; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 148 |  | 
| 6 | 5 |  |  | 5 |  | 24 | use Carp; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 297 |  | 
| 7 | 5 |  |  | 5 |  | 46 | use mro 'c3'; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 31 |  | 
| 8 |  |  |  |  |  |  | use overload( | 
| 9 | 289 |  |  | 289 |  | 22354 | q[""]    => sub { shift->delegate }, | 
| 10 | 5 |  |  |  |  | 66 | fallback => 1, | 
| 11 | 5 |  |  | 5 |  | 365 | ); | 
|  | 5 |  |  |  |  | 12 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw( content file )); | 
| 14 |  |  |  |  |  |  | __PACKAGE__->delegate_class('Path::Class::File'); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $VERSION = '0.58'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 NAME | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | CatalystX::CRUD::Object::File - filesystem CRUD instance | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | package My::File; | 
| 25 |  |  |  |  |  |  | use base qw( CatalystX::CRUD::Object::File ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | 1; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | CatalystX::CRUD::Object::File delegates to Path::Class:File. | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head1 METHODS | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | Only new or overridden methods are documented here. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =cut | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head2 new( file => I<path/to/file> ) | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | Returns new CXCO::File object. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub new { | 
| 46 | 64 |  |  | 64 | 1 | 7764 | my $class = shift; | 
| 47 | 64 |  |  |  |  | 299 | my $self  = $class->next::method(@_); | 
| 48 | 64 | 100 |  |  |  | 22318 | my $file  = $self->{file} or $self->throw_error("file param required"); | 
| 49 | 63 | 100 | 66 |  |  | 348 | $self->{delegate} ||= $self->delegate_class->new( | 
| 50 |  |  |  |  |  |  | ref $file eq 'ARRAY' ? @$file : $file ); | 
| 51 | 63 |  |  |  |  | 6889 | return $self; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =head2 content | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | The contents of the delegate() file object. Set when you call read(). | 
| 57 |  |  |  |  |  |  | Set it yourself and call create() or update() as appropriate to write to the file. | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =cut | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =head2 create | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Writes content() to a file. If the file already exists, will throw_error(), so | 
| 64 |  |  |  |  |  |  | call it like: | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | -s $file ? $file->update : $file->create; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | Returns the number of bytes written. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub create { | 
| 73 | 2 |  |  | 2 | 1 | 463 | my $self = shift; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | # write only if file does not yet exist | 
| 76 | 2 | 50 |  |  |  | 16 | if ( -s $self->delegate ) { | 
| 77 | 0 |  |  |  |  | 0 | return $self->throw_error( | 
| 78 |  |  |  |  |  |  | $self->delegate . " already exists. cannot create()" ); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 2 |  |  |  |  | 196 | return $self->_write; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head2 read | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | Slurp contents of file into content(). No check is performed as to whether | 
| 87 |  |  |  |  |  |  | the file exists, so call like: | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | $file->read if -s $file; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =cut | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub read { | 
| 94 | 44 |  |  | 44 | 1 | 4368 | my $self = shift; | 
| 95 | 44 |  |  |  |  | 128 | $self->{content} = $self->delegate->slurp; | 
| 96 | 44 |  |  |  |  | 11001 | return $self; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head2 update | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | Just like create() only no check is made if the file exists prior to writing | 
| 102 |  |  |  |  |  |  | to it. Returns the number of bytes written. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =cut | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub update { | 
| 107 | 10 |  |  | 10 | 1 | 509 | my $self = shift; | 
| 108 | 10 |  |  |  |  | 35 | return $self->_write; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head2 delete | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | Remove the file from the filesystem. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =cut | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub delete { | 
| 118 | 7 |  |  | 7 | 1 | 17 | my $self = shift; | 
| 119 | 7 |  |  |  |  | 21 | return $self->delegate->remove; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head2 is_new | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Returns true if the file does not yet exist. | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =cut | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub is_new { | 
| 129 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 130 | 0 | 0 |  |  |  | 0 | return defined -s $self->delegate ? 0 : 1; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | sub _write { | 
| 134 | 12 |  |  | 12 |  | 25 | my $self = shift; | 
| 135 | 12 |  |  |  |  | 34 | my $dir  = $self->delegate->dir; | 
| 136 | 12 |  |  |  |  | 170 | $dir->mkpath; | 
| 137 | 12 |  |  |  |  | 1642 | my $fh = $self->delegate->openw(); | 
| 138 | 12 |  |  |  |  | 3415 | print {$fh} $self->content; | 
|  | 12 |  |  |  |  | 61 |  | 
| 139 | 12 |  |  |  |  | 2006 | $fh->close; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | #warn length($self->content) . " bytes written to $self"; | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 12 |  |  |  |  | 1069 | return -s $self->delegate; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head2 serialize | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | Returns the File object as a hashref with 2 keys: file and content. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =cut | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub serialize { | 
| 153 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 154 | 0 |  |  |  |  |  | return { file => $self->file, content => $self->content }; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | 1; | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | __END__ | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 AUTHOR | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Peter Karman, C<< <perl at peknet.com> >> | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head1 BUGS | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | Please report any bugs or feature requests to | 
| 168 |  |  |  |  |  |  | C<bug-catalystx-crud at rt.cpan.org>, or through the web interface at | 
| 169 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD>. | 
| 170 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on | 
| 171 |  |  |  |  |  |  | your bug as I make changes. | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =head1 SUPPORT | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | perldoc CatalystX::CRUD | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | You can also look for information at: | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =over 4 | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | L<http://annocpan.org/dist/CatalystX-CRUD> | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | L<http://cpanratings.perl.org/d/CatalystX-CRUD> | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD> | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =item * Search CPAN | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | L<http://search.cpan.org/dist/CatalystX-CRUD> | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =back | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | Copyright 2007 Peter Karman, all rights reserved. | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 206 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =cut |