blib/lib/App/hopen/Util/BasedPath.pm

filter: Statementstmt 90.9% 50 / 55 Branchbran 50.0% 12 / 24 Conditioncond 53.3% 8 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 94.1% 16 / 17 Podpod 100% 6 / 6 totaltotal 73.8% 93 / 126 CC 18 SCAR 21.6
CC
18
Cov
74%
path_on
30.0
BUILD
11.4
orig
3.1
CRAP 8.6
« blib/lib/App/hopen/Util/XML/FromPerl.pm ↑ Summary blib/lib/App/hopen/Gen/Make/AssetGraphNode.pm »
1 # App::hopen::Util::BasedPath - A path relative to a specified base
2 package App::hopen::Util::BasedPath;
3 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.000013'; # TRIAL
7  
8 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶our @EXPORT; BEGIN { @EXPORT = qw(based_path); }
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 use Class::Tiny qw(path base),
12 {
13 orig_cwd => undef,
14 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # TODO add custom accessors for `path` and `base` to enforce the
16 # type of object instances.
17  
18 # What we use
19 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Getargs::Mixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Docs {{{1
24  
25 =head1 NAME
26  
27 App::hopen::Util::BasedPath - A path relative to a specified base
28  
29 =head1 SYNOPSIS
30  
31 A C<BasedPath> represents a path to a file or directory, plus a directory with
32 respect to which that path is defined. That means you can rebase the file or
33 dir while retaining the relative path. Usage example:
34  
35 my $based = based_path(path => file('foo'), base => dir('bar'));
36 $based->orig; # Path::Class::File for bar/foo
37 $based->path_on(dir('quux')); # Path::Class::File for quux/foo
38  
39 =cut
40  
41 # }}}1
42  
43 =head1 MEMBERS
44  
45 =head2 path
46  
47 The path, as a L<Path::Class::File> or L<Path::Class::Dir> instance.
48 May not be specified as a string when creating a new object, since there's
49 no reliable way to tell whether a file or directory would be intended.
50  
51 This must be a relative path, since the whole point of this module is to
52 combine partial paths!
53  
54 =head2 base
55  
56 A L<Path::Class::Dir> to which the L</path> is relative.
57 May be specified as a string for convenience; however, C<''> (the empty string)
58 is forbidden (to avoid confusion). Use C<dir()> for the current directory
59 or C<dir('')> for the root directory.
60  
61 =head2 orig_cwd
62  
63 The working directory at the time the BasedPath instance was created.
64 This is an absolute path.
65  
66 =head1 FUNCTIONS
67  
68 =head2 is_file
69  
70 Convenience function returning whether L</path> is a L<Path::Class::File>.
71  
72 =cut
73  
74 sub is_file {
75 48 ▶ my ($self) = @_; # NOTE: can't use `my $self = shift` because
Subroutine is_file: called CC=2 SCAR=7.1
Pod documented
76 # that invokes stringification, which causes an
77 # infinite loop when _stringify() calls this.
78 48 ▶ croak 'Need an instance' unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 48 48
79 48 return $self->path->DOES('Path::Class::File');
80 } #is_file()
81  
82 =head2 orig
83  
84 Returns a C<Path::Class::*> representing L</path> relative to L</base>, i.e.,
85 the original location.
86  
87 =cut
88  
89 sub orig {
90 18 ▶ my ($self) = @_;
Subroutine orig: called CC=3 SCAR=11.4
Pod documented
91 18 ▶ croak 'Need an instance' unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 18 18
92  
93 18 ▶ my $classname = $self->is_file ? 'Path::Class::File' : 'Path::Class::Dir';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_file ? : 18 0 18
94 18 return $classname->new(
95 $self->base->components,
96 $self->path->components
97 );
98 } #orig()
99  
100 =head2 path_wrt
101  
102 Returns a C<Path::Class::*> representing the relative path from a given
103 directory to the original location. (C<wrt> = With Respect To) Example:
104  
105 # In directory "project"
106 my $based = based_path(path => file('foo'), base => dir('bar'));
107 $based->orig; # Path::Class::File for bar/foo
108 $based->path_wrt('..'); # Path::Class::File for project/bar/foo
109  
110 =cut
111  
112 sub path_wrt {
113 17 ▶ my ($self, %args) = parameters('self',['whence'], @_);
Subroutine path_wrt: called CC=1 SCAR=0.0
Pod documented
114 17 return $self->orig->relative($args{whence});
115 } #path_wrt()
116  
117 =head2 path_on
118  
119 my $new_path = $based_path->path_on($new_base);
120  
121 Given a L<Path::Class::Dir>, return a C<Path::Class::*> instance representing
122 L</path>, but relative to C<$new_base> instead of to L</base>.
123  
124 This is in some ways the opposite of C<Path::Class::File::relative()>:
125  
126 # in directory 'dir'
127 my $file = file('foo.txt'); # The foo.txt in dir/
128 say $file->relative('..'); # "dir/foo.txt" - same file, but
129 # accessed from "..".
130  
131 my $based = based_path(path=>file('foo.txt'), base=>'');
132 # Name foo.txt, based off dir
133 say $based->path_on(dir('..')); # dir/../foo.txt - a different file
134  
135 =cut
136  
137 sub path_on {
138 0 ▶ my ($self, $new_base) = @_;
Subroutine path_on: not called CC=5 SCAR=34.0
Pod documented
139 0 ▶ croak 'Need an instance' unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
140 0 ▶ croak 'Need a new base path' unless ref($new_base) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new_base and $new_base->DOES("Path::Class::Dir") 0 0 0
Condition0%0/3
ref $new_base and $new_base->DOES("Path::Class::Dir")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $new_base and $new_base->DOES("Path::Class::Dir")
ref $new_base $new_base->DOES("Path::Class::Dir")
Truth table0%0/3
A and B
A= ref $new_baseB= $new_base->DOES("Path::Class::Dir")
ABresult
0X0
100
111
141 $new_base->DOES('Path::Class::Dir');
142  
143 0 ▶ my $classname = $self->is_file ? 'Path::Class::File' : 'Path::Class::Dir';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_file ? : 0 0 0
144 0 return $classname->new(
145 $new_base->components,
146 $self->path->components
147 );
148 } #path_on()
149  
150 =head2 _stringify
151  
152 Stringify the instance in a way that is human-readable, but NOT suitable
153 for machine consumption.
154  
155 =cut
156  
157 sub _stringify {
158 30 ▶ my ($self, $other, $swap) = @_;
Subroutine _stringify: called CC=2 SCAR=6.9
159 30 ▶ return ('``' . ($self->base) . "'' hosting " .
Branch: 1 branch
BranchTrueFalseTotal
$self->is_file ? : 29 1 30
160 ($self->is_file ? 'file ``': 'dir ``') .
161 ($self->path) . "''"
162 );
163 } #_stringify()
164  
165 use overload
166 3 '""' => '_stringify',
167 3 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 =head2 BUILD
170  
171 Sanity-check the arguments.
172  
173 =cut
174  
175 sub BUILD {
176 15 ▶ my ($self) = @_;
Subroutine BUILD: called CC=10 SCAR=24.3
Pod documented
177 15 ▶ die 'Need an instance' unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 15 15
178  
179 # --- path ---
180 15 ▶ croak "path must be a Path::Class::*" unless $self->path &&
Branch: 1 branch
BranchTrueFalseTotal
unless $self->path and $self->path->DOES("Path::Class::Dir") || $self->path->DOES("Path::Class::File") 0 15 15
Condition66%4/6
$self->path and $self->path->DOES("Path::Class::Dir") || $self->path->DOES("Path::Class::File")
!ll&&!rl&&r
0510
$self->path->DOES("Path::Class::Dir") or $self->path->DOES("Path::Class::File")
l!l&&r!l&&!r
5100
MC/DC0%0/3
$self->path and $self->path->DOES("Path::Class::Dir") || $self->path->DOES("Path::Class::File")
$self->path $self->path->DOES("Path::Class::Dir") $self->path->DOES("Path::Class::File")
Truth table25%1/4
A and B or C
A= $self->pathB= $self->path->DOES("Path::Class::Dir")C= $self->path->DOES("Path::Class::File")
ABCresult
0XX0
1000
11X1
1011
181 ($self->path->DOES('Path::Class::Dir') ||
182 $self->path->DOES('Path::Class::File'));
183 15 ▶ croak "path must be relative" unless $self->path->is_relative;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->path->is_relative 1 14 15
184  
185 # --- base ---
186 # Accept strings as base for convenience
187 14 ▶ $self->base( dir($self->base) ) if !ref($self->base) && $self->base ne '';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $self->base and $self->base ne "" 0 14 14
Condition66%2/3
not ref $self->base and $self->base ne ""
!ll&&!rl&&r
1310
MC/DC0%0/2
not ref $self->base and $self->base ne ""
not ref $self->base $self->base ne ""
Truth table66%2/3
not(A) and B
A= not ref $self->baseB= $self->base ne ""
ABresult
0X0
100
111
188  
189 14 ▶ croak "base must be a Path::Class::Dir" unless $self->base &&
Branch: 1 branch
BranchTrueFalseTotal
unless $self->base and $self->base->DOES("Path::Class::Dir") 1 13 14
Condition66%2/3
$self->base and $self->base->DOES("Path::Class::Dir")
!ll&&!rl&&r
1013
MC/DC50%1/2
$self->base and $self->base->DOES("Path::Class::Dir")
$self->base $self->base->DOES("Path::Class::Dir")
Truth table66%2/3
A and B
A= $self->baseB= $self->base->DOES("Path::Class::Dir")
ABresult
0X0
100
111
190 $self->base->DOES('Path::Class::Dir');
191 # TODO? make base absolute??
192  
193 # --- orig_cwd ---
194 13 $self->orig_cwd(dir()->absolute);
195  
196 } #BUILD()
197  
198 =head1 STATIC FUNCTIONS
199  
200 =head2 based_path
201  
202 A synonym for C<< App::hopen::Util::BasedPath->new() >>. Exported by default.
203  
204 =cut
205  
206 sub based_path {
207 12 ▶ return __PACKAGE__->new(@_);
Subroutine based_path: called CC=1 SCAR=0.0
Pod documented
208 } #based_path()
209  
210 3 1;
211 __END__
212 # vi: set fdm=marker: #
« blib/lib/App/hopen/Util/XML/FromPerl.pm ↑ Summary blib/lib/App/hopen/Gen/Make/AssetGraphNode.pm »