lib/MouseX/ConfigFromFile.pm

filter: Statementstmt 96.3% 26 / 27 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 93.0% 40 / 43 CC 6 SCAR 14.0
CC
6
Cov
92%
new_with_config
6.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.1
↑ Summary
1 package MouseX::ConfigFromFile;
2  
3 4 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use MouseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.05';
8  
9 4 requires 'get_config_from_file';
10  
11 4 has 'configfile' => (
12 is => 'ro',
13 isa => 'Path::Class::File',
14 coerce => 1,
15 predicate => 'has_configfile',
16 );
17  
18 4 ▶sub new_with_config {
Subroutine new_with_config: called CC=6 SCAR=18.1
Pod documented
19 4 my ($class, %params) = @_;
20  
21 4 ▶ my $file = defined $params{configfile} ? $params{configfile} : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'configfile'} ? : 1 3 4
22 3 my $attr = $class->meta->get_attribute('configfile');
23 3 ▶ if ($attr->has_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->has_default) { } 2 1 3
elsif ($attr->has_builder) { } 1 0 1
24 2 my $default = $attr->default;
25 2 ▶ ref($default) eq 'CODE' ? $default->($class) : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 1 1 2
26 }
27 elsif ($attr->has_builder) {
28 1 my $builder = $attr->builder;
29 1 $class->$builder();
30 }
31 else {
32 0 undef;
33 }
34 };
35  
36 my %args = (
37 4 ▶ defined $file ? %{ $class->get_config_from_file($file) } : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 4 0 4
38 %params,
39 );
40  
41 4 return $class->new(%args);
42 }
43  
44 4 ▶no Mouse::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 =head1 NAME
47  
48 MouseX::ConfigFromFile - An abstract Mouse role for setting attributes from a configfile
49  
50 =head1 SYNOPSIS
51  
52 A real role based on this abstract role:
53  
54 package MyApp::ConfigRole;
55 use Mouse::Role;
56 with 'MouseX::ConfigFromFile';
57  
58 use MyApp::ConfigLoader;
59  
60 sub get_config_from_file {
61 my ($class, $file) = @_;
62  
63 my $config_hashref = MyApp::ConfigLoader->load($file);
64  
65 return $config_hashref;
66 }
67  
68 A class that uses it:
69  
70 package MyApp;
71 use Mouse;
72 with 'MyApp::ConfigRole';
73  
74 # optionally, default the configfile:
75 has '+configfile' => ( default => '/tmp/myapp.yml' );
76  
77 A script that uses the class with a configfile:
78  
79 my $app = MyApp->new_with_config(
80 configfile => '/etc/myapp.yml',
81 other_opt => 'foo',
82 );
83  
84 =head1 DESCRIPTION
85  
86 This is an abstract role which provides an alternate constructor for
87 creating objects using parameters passed in from a configuration file.
88 The actual implementation of reading the configuration file is left to
89 concrete subroles.
90  
91 It declares an attribute C<configfile> and a class method
92 C<new_with_config>, and requires that concrete roles derived from it
93 implement the class method C<get_config_from_file>.
94  
95 Attributes specified directly as arguments to C<new_with_config>
96 supercede those in the configfile.
97  
98 =head1 METHODS
99  
100 =head2 new_with_config(%params?)
101  
102 This is an alternate constructor, which knows to look for the
103 C<configfile> option in its arguments and use that to set attributes.
104 It is much like L<MouseX::Getopts>' C<new_with_options>.
105  
106 Example:
107  
108 my $app = MyApp->new_with_config( configfile => '/etc/foo.yaml' );
109  
110 Explicit arguments will override anything set by the configfile.
111  
112 =head2 get_config_from_file($file)
113  
114 This method is not implemented in this role, but it is required
115 of all subroles. Its two arguments are the class name and the configfile,
116 and it is expected to return a hashref of arguments to pass to C<new()>
117 which are sourced from the configfile.
118  
119 Example:
120  
121 sub get_config_from_file {
122 my ($class, $file) = @_;
123  
124 my $config = {};
125  
126 # ... load config from $file ...
127  
128 return $config;
129 }
130  
131 =head1 PROPERTIES
132  
133 =head2 configfile
134  
135 This is a L<Path::Class::File> object which can be coerced from a regular
136 path name string. This is the file your attributes are loaded from.
137 You can add a default configfile in the class using the role and it will
138 be honored at the appropriate time:
139  
140 has '+configfile' => ( default => '/etc/myapp.yaml' );
141  
142 =head1 AUTHOR
143  
144 NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
145  
146 =head1 THANKS TO
147  
148 Brandon L. Black, L<MooseX::ConfigFromFile/AUTHOR>
149  
150 =head1 LICENSE
151  
152 This library is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself.
154  
155 =head1 SEE ALSO
156  
157 L<Mouse>, L<Mouse::Role>, L<MouseX::Types::Path::Class>, L<MooseX::ConfigFromFile>
158  
159 =cut
↑ Summary