line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MouseX::ConfigFromFile; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22
|
use 5.008_001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
923
|
use Mouse::Role; |
|
1
|
|
|
|
|
37244
|
|
|
1
|
|
|
|
|
5
|
|
5
|
1
|
|
|
1
|
|
852
|
use MouseX::Types::Path::Class; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires 'get_config_from_file'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'configfile' => ( |
12
|
|
|
|
|
|
|
is => 'ro', |
13
|
|
|
|
|
|
|
isa => 'Path::Class::File', |
14
|
|
|
|
|
|
|
coerce => 1, |
15
|
|
|
|
|
|
|
predicate => 'has_configfile', |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new_with_config { |
19
|
|
|
|
|
|
|
my ($class, %params) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $file = defined $params{configfile} ? $params{configfile} : do { |
22
|
|
|
|
|
|
|
my $attr = $class->meta->get_attribute('configfile'); |
23
|
|
|
|
|
|
|
if ($attr->has_default) { |
24
|
|
|
|
|
|
|
my $default = $attr->default; |
25
|
|
|
|
|
|
|
ref($default) eq 'CODE' ? $default->($class) : $default; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
elsif ($attr->has_builder) { |
28
|
|
|
|
|
|
|
my $builder = $attr->builder; |
29
|
|
|
|
|
|
|
$class->$builder(); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
else { |
32
|
|
|
|
|
|
|
undef; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my %args = ( |
37
|
|
|
|
|
|
|
defined $file ? %{ $class->get_config_from_file($file) } : (), |
38
|
|
|
|
|
|
|
%params, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
return $class->new(%args); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
no Mouse::Role; 1; |
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 and a class method |
92
|
|
|
|
|
|
|
C, and requires that concrete roles derived from it |
93
|
|
|
|
|
|
|
implement the class method C. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Attributes specified directly as arguments to C |
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 option in its arguments and use that to set attributes. |
104
|
|
|
|
|
|
|
It is much like L' C. |
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 |
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 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 Emasaki@cpan.orgE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 THANKS TO |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Brandon L. Black, L |
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, L, L, L |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |