Proxy/ConfigFromFile.pm

filter: Statementstmt 47.0% 8 / 17 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 2 / 3 Podpod 100% 1 / 1 totaltotal 44.0% 11 / 25 CC 3 SCAR 20.3
CC
3
Cov
38%
new_with_config
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.6
« Command/PayloadClass.pm ↑ Summary Command/Hello.pm »
1 package SRS::EPP::Proxy::ConfigFromFile;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 with 'MooseX::ConfigFromFile', { -excludes => "new_with_config" };
5  
6 sub new_with_config {
7 0 ▶ my ($class, %opts) = @_;
Subroutine new_with_config: not called CC=3 SCAR=24.8
Pod documented
8  
9 0 my $configfile;
10  
11 0 ▶ if(defined $opts{configfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'configfile'}) { } 0 0 0
12 $configfile = $opts{configfile}
13 0 }
14 else {
15 0 $configfile = $class->configfile
16 }
17  
18 0 ▶ if(defined $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configfile) 0 0 0
19 0 %opts = (%{$class->get_config_from_file($configfile)}, %opts);
20 }
21  
22 0 $class->new(%opts);
23 }
24  
25 1 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 __END__
28  
29 =pod
30  
31 =head1 NAME
32  
33 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
34  
35 =head1 SYNOPSIS
36  
37 ########
38 ## A real role based on this abstract role:
39 ########
40  
41 package MooseX::SomeSpecificConfigRole;
42 use Moose::Role;
43
44 with 'MooseX::ConfigFromFile';
45
46 use Some::ConfigFile::Loader ();
47  
48 sub get_config_from_file {
49 my ($class, $file) = @_;
50  
51 my $options_hashref = Some::ConfigFile::Loader->load($file);
52  
53 return $options_hashref;
54 }
55  
56  
57 ########
58 ## A class that uses it:
59 ########
60 package Foo;
61 use Moose;
62 with 'MooseX::SomeSpecificConfigRole';
63  
64 # optionally, default the configfile:
65 has +configfile ( default => '/tmp/foo.yaml' );
66  
67 # ... insert your stuff here ...
68  
69 ########
70 ## A script that uses the class with a configfile
71 ########
72  
73 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
74  
75 =head1 DESCRIPTION
76  
77 This is an abstract role which provides an alternate constructor for creating
78 objects using parameters passed in from a configuration file. The
79 actual implementation of reading the configuration file is left to
80 concrete subroles.
81  
82 It declares an attribute C<configfile> and a class method C<new_with_config>,
83 and requires that concrete roles derived from it implement the class method
84 C<get_config_from_file>.
85  
86 Attributes specified directly as arguments to C<new_with_config> supercede those
87 in the configfile.
88  
89 L<MooseX::Getopt> knows about this abstract role, and will use it if available
90 to load attributes from the file specified by the commandline flag C<--configfile>
91 during its normal C<new_with_options>.
92  
93 =head1 Attributes
94  
95 =head2 configfile
96  
97 This is a L<Path::Class::File> object which can be coerced from a regular pathname
98 string. This is the file your attributes are loaded from. You can add a default
99 configfile in the class using the role and it will be honored at the appropriate time:
100  
101 has +configfile ( default => '/etc/myapp.yaml' );
102  
103 =head1 Class Methods
104  
105 =head2 new_with_config
106  
107 This is an alternate constructor, which knows to look for the C<configfile> option
108 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
109 C<new_with_options>. Example:
110  
111 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
112  
113 Explicit arguments will overide anything set by the configfile.
114  
115 =head2 get_config_from_file
116  
117 This class method is not implemented in this role, but it is required of all subroles.
118 Its two arguments are the classname and the configfile, and it is expected to return
119 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
120  
121 =head2 meta
122  
123 The Moose meta stuff, included here because otherwise pod tests fail sometimes
124  
125 =head1 BUGS
126  
127 =head1 AUTHOR
128  
129 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
130  
131 =head1 LICENSE
132  
133 This library is free software; you can redistribute it and/or modify
134 it under the same terms as Perl itself.
135  
136 =cut
« Command/PayloadClass.pm ↑ Summary Command/Hello.pm »