| 1 | package Giddy::Role::PathMaker; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use Any::Moose 'Role'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use File::Path qw/make_path/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = "0.013_001"; | |||||||||
| 10 | 2 | $VERSION = eval $VERSION; | |||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Giddy::Role::PathMaker - Provides file and directory creation for Giddy::Database | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | version 0.013_001 | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | # used internally | ||||||||||
| 23 | |||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||
| 25 | |||||||||||
| 26 | This role provides L<Giddy::Database> with common methods needed for creating | ||||||||||
| 27 | paths in the database. | ||||||||||
| 28 | |||||||||||
| 29 | Requires the attribute '_repo' to be implemented by consuming classes. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 2 | requires '_repo'; | |||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | =head2 _create_dir( $path ) | ||||||||||
| 38 | |||||||||||
| 39 | Creates a directory under C<$path> and chmods it 0775. If path already exists, | ||||||||||
| 40 | nothing will happen. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub _create_dir { | ||||||||
|
Subroutine
_create_dir: not called CC=1 SCAR=6.9
| |||||||||||
| 45 | 0 | my ($self, $path) = @_; | |||||||||
| 46 | |||||||||||
| 47 | 0 | make_path($self->_repo->work_tree.'/'.$path, { mode => 0775 }); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =head2 _mark_dir_as_static( $path ) | ||||||||||
| 51 | |||||||||||
| 52 | Marks a path (assumed to be a directory) as a static-file directory by creating | ||||||||||
| 53 | an empty '.static' file under it. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub _mark_dir_as_static { | ||||||||
|
Subroutine
_mark_dir_as_static: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 0 | my ($self, $path) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 0 | $self->_touch($path.'/.static'); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =head2 _touch( $path ) | ||||||||||
| 64 | |||||||||||
| 65 | Creates an empty file in C<$path> and chmods it 0664. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | 1 | ▶ | sub _touch { | ||||||||
|
Subroutine
_touch: called CC=2 SCAR=7.0
| |||||||||||
| 70 | 1 | my ($self, $path) = @_; | |||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | open(FILE, ">:utf8", $self->_repo->work_tree.'/'.$path) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | || croak "Can't _touch $path: $!"; | ||||||||||
| 74 | 1 | close FILE; | |||||||||
| 75 | 1 | chmod(0664, $self->_repo->work_tree.'/'.$path); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | =head2 _create_file( $path, $content, $mode ) | ||||||||||
| 79 | |||||||||||
| 80 | Creates a file called C<$path>, with the provided content, and chmods it to | ||||||||||
| 81 | C<$mode>. | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub _create_file { | ||||||||
|
Subroutine
_create_file: not called CC=3 SCAR=24.8
| |||||||||||
| 86 | 0 | my ($self, $path, $content, $mode) = @_; | |||||||||
| 87 | |||||||||||
| 88 | # there's no need to open the output file in binary :utf-8 mode, | ||||||||||
| 89 | # as the YAML Dump() function returns UTF-8 encoded data (so it seems) | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | open(FILE, '>', $self->_repo->work_tree.'/'.$path) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | || croak "Can't open file $path for writing: $!"; | ||||||||||
| 93 | 0 | flock(FILE, 2); | |||||||||
| 94 | 0 | print FILE $content; | |||||||||
| 95 | 0 | ▶ | close(FILE) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | || carp "Error closing file $path: $!"; | ||||||||||
| 97 | 0 | chmod($mode, $self->_repo->work_tree.'/'.$path); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | =head1 AUTHOR | ||||||||||
| 101 | |||||||||||
| 102 | Ido Perlmuter, C<< <ido at ido50.net> >> | ||||||||||
| 103 | |||||||||||
| 104 | =head1 BUGS | ||||||||||
| 105 | |||||||||||
| 106 | Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through | ||||||||||
| 107 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll | ||||||||||
| 108 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 109 | |||||||||||
| 110 | =head1 SUPPORT | ||||||||||
| 111 | |||||||||||
| 112 | You can find documentation for this module with the perldoc command. | ||||||||||
| 113 | |||||||||||
| 114 | perldoc Giddy::Role::PathMaker | ||||||||||
| 115 | |||||||||||
| 116 | You can also look for information at: | ||||||||||
| 117 | |||||||||||
| 118 | =over 4 | ||||||||||
| 119 | |||||||||||
| 120 | =item * RT: CPAN's request tracker | ||||||||||
| 121 | |||||||||||
| 122 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy> | ||||||||||
| 123 | |||||||||||
| 124 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 125 | |||||||||||
| 126 | L<http://annocpan.org/dist/Giddy> | ||||||||||
| 127 | |||||||||||
| 128 | =item * CPAN Ratings | ||||||||||
| 129 | |||||||||||
| 130 | L<http://cpanratings.perl.org/d/Giddy> | ||||||||||
| 131 | |||||||||||
| 132 | =item * Search CPAN | ||||||||||
| 133 | |||||||||||
| 134 | L<http://search.cpan.org/dist/Giddy/> | ||||||||||
| 135 | |||||||||||
| 136 | =back | ||||||||||
| 137 | |||||||||||
| 138 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 139 | |||||||||||
| 140 | Copyright 2011 Ido Perlmuter. | ||||||||||
| 141 | |||||||||||
| 142 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 143 | under the terms of either: the GNU General Public License as published | ||||||||||
| 144 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 145 | |||||||||||
| 146 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 147 | |||||||||||
| 148 | =cut | ||||||||||
| 149 | |||||||||||
| 150 | 2 | 1; | |||||||||