lib/Badger/Workplace.pm

filter: Statementstmt 81.4% 22 / 27 Branchbran 37.5% 3 / 8 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.3% 5 / 6 Podpod 80.0% 4 / 5 totaltotal 56.1% 41 / 73 CC 12 SCAR 25.8
CC
12
Cov
60%
init_workplace
20.5
dir
6.0
uri
2.0
CRAP 13.2
« blib/lib/Badger/Factory/Class.pm ↑ Summary lib/Badger/Workspace.pm »
1 package Badger::Workplace;
2  
3 use Badger::Class
4 4 version => 0.01,
5 debug => 0,
6 base => 'Badger::Base',
7 import => 'class',
8 utils => 'Dir resolve_uri', # resolve_uri truelike falselike params self_params extend',
9 constants => 'SLASH',
10 accessors => 'root urn',
11 alias => {
12 directory => \&dir,
13 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 #-----------------------------------------------------------------------------
17 # Initialisation methods
18 #-----------------------------------------------------------------------------
19  
20 sub init {
21 1 ▶ my ($self, $config) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
22 1 $self->init_workplace($config);
23 1 return $self;
24 }
25  
26 sub init_workplace {
27 1 ▶ my ($self, $config) = @_;
Subroutine init_workplace: called CC=10 SCAR=30.2
Pod undocumented
28  
29 # The mkdir flag is used to indicate the special case where the root
30 # directory (and perhaps other support files, data, etc) don't yet exist
31 # because some other bit of code is in the process of creating it anew.
32 1 ▶ my $mkdir = $config->{ mkdir } || 0;
Condition50%1/2
$config->{'mkdir'} || 0
l!l
01
MC/DC0%0/1
$config->{'mkdir'} || 0
$config->{'mkdir'}
Truth table50%1/2
A
A= $config->{'mkdir'}
Aresult
11
00
33  
34 # The filespec can be specified to provide a hash of options for files
35 1 ▶ my $filespec = $config->{ filespec } || { };
Condition50%1/2
$config->{'filespec'} || {}
l!l
01
MC/DC0%0/1
$config->{'filespec'} || {}
$config->{'filespec'}
Truth table50%1/2
A
A= $config->{'filespec'}
Aresult
11
00
36  
37 # The root directory must exist unless this is a neophyte in which case
38 # we can create the directory.
39 my $dir = $config->{ root }
40 || $config->{ dir }
41 || $config->{ directory }
42 1 ▶ || return $self->error_msg( missing => 'root directory' );
Condition37%3/8
$config->{'root'} || $config->{'dir'} || $config->{'directory'} || (return $self->error_msg('missing', 'root directory'))
l!l
10
$config->{"root"} or $config->{"dir"} or $config->{"directory"}
l!l&&r!l&&!r
100
$config->{"root"} or $config->{"dir"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$config->{'root'} || $config->{'dir'} || $config->{'directory'} || (return $self->error_msg('missing', 'root directory'))
$config->{"root"} $config->{"dir"} $config->{"directory"}
Truth table25%1/4
A or B or C
A= $config->{"root"}B= $config->{"dir"}C= $config->{"directory"}
ABCresult
1XX1
01X1
0011
0000
43 1 my $root = Dir($dir, $filespec);
44  
45 1 ▶ if (! $root->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($root->exists) 0 1 1
46 0 ▶ if ($mkdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mkdir) { } 0 0 0
47 0 $root->mkdir;
48 }
49 else {
50 0 return $self->error_msg( invalid => root => $dir );
51 }
52 }
53  
54 1 $self->{ root } = $root;
55 1 ▶ $self->{ urn } = $config->{ urn } // $root->name;
Condition33%1/3
$config->{"urn"} // $root->name
l!l&&r!l&&!r
010
MC/DC0%0/2
$config->{"urn"} // $root->name
$config->{"urn"} $root->name
Truth table33%1/3
A // B
A= $config->{"urn"}B= $root->name
ABresult
1X1
011
000
56 1 ▶ $self->{ uri } = $config->{ uri } // $self->{ urn };
Condition33%1/3
$config->{"uri"} // $self->{"urn"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$config->{"uri"} // $self->{"urn"}
$config->{"uri"} $self->{"urn"}
Truth table33%1/3
A // B
A= $config->{"uri"}B= $self->{"urn"}
ABresult
1X1
011
000
57 1 $self->{ mkdir } = $mkdir;
58  
59 1 return $self;
60 }
61  
62 #-----------------------------------------------------------------------------
63 # Methods for accessing directories and files relative to the workplace root
64 #-----------------------------------------------------------------------------
65  
66 sub dir {
67 0 ▶ my $self = shift;
Subroutine dir: not called CC=2 SCAR=17.9
Pod documented
68 return @_
69 0 ▶ ? $self->root->dir(@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
70 : $self->root;
71 }
72  
73 sub file {
74 1 ▶ my $self = shift;
Subroutine file: called CC=1 SCAR=0.0
Pod documented
75 1 return $self->root->file(@_);
76 }
77  
78 sub uri {
79 2 ▶ my $self = shift;
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
80 return @_
81 ? sprintf("%s%s", $self->{ uri }, resolve_uri(SLASH, @_))
82 2 ▶ : $self->{ uri };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
83 }
84  
85 4 1;
86  
87 =head1 NAME
88  
89 Badger::Workplace - a place to do work
90  
91 =head1 DESCRIPTION
92  
93 This is a very simple base class for modules that operate on or around
94 a particular filesystem directory. See L<Badger::Config::Filesystem> for an
95 example of it in us.
96  
97 =head1 CONFIGURATION OPTIONS
98  
99 =head2 root / dir / directory
100  
101 Any of C<root>, C<dir> or C<directory> can be provided to specify the root
102 directory of the workplace.
103  
104 =head2 urn
105  
106 This option can be set to define a Universal Resource Name (URN) for the
107 workplace for reference purposes. If undefined it defaults to the name of
108 the root directory.
109  
110 =head2 uri
111  
112 This option can be set to define a Universal Resource Identifier (URN) for the
113 workplace for reference purposes. If undefined it defaults to the name of
114 the value of L<urn>.
115  
116 =head2 mkdir
117  
118 The object constructor will fail if the root directory specified via L<root>
119 (or C<dir> or C<directory>) does not exist. Alternately, set the C<mkdir>
120 option to any true value and the directory will be created automatically.
121  
122 =head1 METHODS
123  
124 =head2 dir($name) / directory($name)
125  
126 Returns a L<Badger::Filesystem::Directory> object for a named sub-directory
127 relative to the workplace root.
128  
129 When called with any arguments it returns a L<Badger::Filesystem::Directory>
130 object for the workplace root directory.
131  
132 =head2 file($name)
133  
134 Returns a L<Badger::Filesystem::File> object for a named files
135 relative to the workplace root.
136  
137 =head2 uri($path)
138  
139 When called without any arguments this method returns the base URI for the
140 workspace.
141  
142 print $workspace->uri; # e.g. foo
143  
144 When called with a relative URI path as an argument, it returns the URI
145 resolved relative to the project base URI.
146  
147 print $workspace->uri('bar'); # e.g. foo/bar
148  
149 =head1 AUTHOR
150  
151 Andy Wardley L<http://wardley.org/>
152  
153 =head1 COPYRIGHT
154  
155 Copyright (C) 2008-2014 Andy Wardley. All Rights Reserved.
156  
157 This module is free software; you can redistribute it and/or modify it
158 under the same terms as Perl itself.
159  
160 =cut
« blib/lib/Badger/Factory/Class.pm ↑ Summary lib/Badger/Workspace.pm »