Environment/Role.pm

filter: Statementstmt 91.3% 53 / 58 Branchbran 57.1% 8 / 14 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 78.5% 84 / 107 CC 12 SCAR 14.7
CC
12
Cov
80%
_build_project_environment
9.0
_build_project_root
5.9
_module_path
2.3
CRAP 4.3
↑ Summary Environment.pm »
1 package Project::Environment::Role;
2  
3 # ABSTRACT: Moose role for Project::Environment
4  
5 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooseX::Types::Path::Tiny qw(Path);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Class::Inspector qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::FindDev qw(find_dev);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Module::Path qw(module_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 1 has project_root => (
18 is => 'ro',
19 isa => Path,
20 coerce => 1,
21 lazy => 1,
22 builder => '_build_project_root',
23 );
24  
25 5 ▶sub _build_project_root {
Subroutine _build_project_root: called CC=5 SCAR=17.8
26 5 my $self = shift;
27  
28 5 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
29  
30 5 ▶ my $file
Branch: 1 branch
BranchTrueFalseTotal
$class eq 'Project::Environment' ? : 1 4 5
31 = $class eq 'Project::Environment'
32 ? $self->_caller->[1]
33 : $self->_module_path;
34  
35 5 my $dir = find_dev(path($file)->dirname);
36  
37 5 ▶ unless ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 5 5
38 0 $dir = find_dev(File::Spec->curdir());
39 }
40  
41 5 ▶ unless ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 5 5
42 0 Carp::croak(
43 q{}
44 . 'Cannot build project_root. '
45 . 'Please set project_root attribute by hand or create one of '
46 . 'the project_root_files in the root of the project.',
47 );
48 }
49  
50 5 return $dir;
51 }
52  
53  
54 4 ▶sub _module_path {
Subroutine _module_path: called CC=2 SCAR=8.1
55 4 my ($self,) = @_;
56  
57 4 ▶ return module_path(ref $self || $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
58 }
59  
60  
61 1 has environment_filename => (
62 is => 'ro',
63 isa => 'Str',
64 default => '.environment',
65 );
66  
67  
68 1 has environment_path => (
69 is => 'ro',
70 isa => Path,
71 coerce => 1,
72 lazy => 1,
73 builder => '_build_environment_path',
74 );
75  
76 5 ▶sub _build_environment_path {
Subroutine _build_environment_path: called CC=1 SCAR=0.0
77 5 my $self = shift;
78  
79 5 return $self->project_root->child($self->environment_filename);
80 }
81  
82  
83 1 has default_environment => (
84 is => 'ro',
85 isa => 'Str',
86 predicate => 'has_default_environment',
87 );
88  
89  
90 1 has environment_variable => (
91 is => 'ro',
92 isa => 'Str',
93 default => 'PROJECT_ENVIRONMENT',
94 );
95  
96  
97 1 has project_environment => (
98 is => 'ro',
99 isa => 'Str',
100 lazy => 1,
101 builder => '_build_project_environment',
102 );
103  
104 6 ▶sub _build_project_environment {
Subroutine _build_project_environment: called CC=7 SCAR=22.0
105 6 my $self = shift;
106  
107 ## see if %ENV is set
108 6 my $ev = $self->environment_variable;
109 6 ▶ if (exists $ENV{$ev} && defined $ENV{$ev} && $ENV{$ev}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$ev} and defined $ENV{$ev} and $ENV{$ev}) 1 5 6
Condition66%4/6
exists $ENV{$ev} and defined $ENV{$ev} and $ENV{$ev}
!ll&&!rl&&r
501
exists $ENV{$ev} and defined $ENV{$ev}
!ll&&!rl&&r
501
MC/DC33%1/3
exists $ENV{$ev} and defined $ENV{$ev} and $ENV{$ev}
exists $ENV{$ev} defined $ENV{$ev} $ENV{$ev}
Truth table50%2/4
A and B and C
A= exists $ENV{$ev}B= defined $ENV{$ev}C= $ENV{$ev}
ABCresult
0XX0
10X0
1100
1111
110 1 return $ENV{$ev};
111 }
112  
113 ## now check .environment file
114 5 ▶ if (-e $self->environment_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->environment_path) 5 0 5
115 5 my $env = scalar $self->environment_path->slurp;
116  
117 5 ▶ if ($env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 5 0 5
118 5 chomp($env);
119 5 return $env;
120 }
121 }
122  
123 ## finally, try default
124 0 ▶ if ($self->has_default_environment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_default_environment) 0 0 0
125 0 return $self->default_environment;
126 }
127  
128 Carp::croak(
129 0 q{}
130 . 'Cannot find environment file at '
131 . $self->environment_path
132 . ' and no default_environment is set.',
133 );
134 }
135  
136  
137 1 ▶sub environment {
Subroutine environment: called CC=1 SCAR=0.0
Pod documented
138 1 return shift->project_environment;
139 }
140  
141  
142 1 ▶sub env {
Subroutine env: called CC=1 SCAR=0.0
Pod documented
143 1 return shift->project_environment;
144 }
145  
146 1 1; ## eof
147  
148 __END__
↑ Summary Environment.pm »