| 1 | package MooseX::ConfigFromFile; # git description: v0.13-18-g507c9de | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: An abstract Moose role for setting attributes from a configfile | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 6 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use MooseX::Types::Path::Tiny 0.005 'Path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use MooseX::Types::Moose 'Undef'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | requires 'get_config_from_file'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has configfile => ( | ||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => Path|Undef, | ||||||||||||||||||||||||||||
| 17 | coerce => 1, | ||||||||||||||||||||||||||||
| 18 | predicate => 'has_configfile', | ||||||||||||||||||||||||||||
| 19 | eval "require MooseX::Getopt; 1" ? (traits => ['Getopt']) : (), | ||||||||||||||||||||||||||||
| 20 | lazy => 1, | ||||||||||||||||||||||||||||
| 21 | # it sucks that we have to do this rather than using a builder, but some old code | ||||||||||||||||||||||||||||
| 22 | # simply swaps in a new default sub into the attr definition | ||||||||||||||||||||||||||||
| 23 | 9 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 9 | ▶ | $class->_get_default_configfile if $class->can('_get_default_configfile'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||
| 27 | 6 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 16 | ▶ | sub new_with_config { | ||||||||||||||||||||||||||
|
Subroutine
new_with_config: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 16 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 16 | my $configfile; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 16 | ▶ | if(defined $opts{configfile}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | $configfile = $opts{configfile} | ||||||||||||||||||||||||||||
| 36 | 0 | } | |||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||
| 38 | # This would only succeed if the consumer had defined a new configfile | ||||||||||||||||||||||||||||
| 39 | # sub to override the generated reader - as suggested in old | ||||||||||||||||||||||||||||
| 40 | # documentation -- or if $class is an instance not a class name | ||||||||||||||||||||||||||||
| 41 | 16 | $configfile = eval { $class->configfile }; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # this is gross, but since a lot of users have swapped in their own | ||||||||||||||||||||||||||||
| 44 | # default subs, we have to keep calling it rather than calling a | ||||||||||||||||||||||||||||
| 45 | # builder sub directly - and it might not even be a coderef either | ||||||||||||||||||||||||||||
| 46 | 16 | my $cfmeta = $class->meta->find_attribute_by_name('configfile'); | |||||||||||||||||||||||||||
| 47 | 16 | ▶ | $configfile = $cfmeta->default if not defined $configfile and $cfmeta->has_default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $configfile and $cfmeta->has_default
MC/DC50%1/2
not defined $configfile and $cfmeta->has_default
not defined $configfile
$cfmeta->has_default
Truth table66%2/3
not(A) and B
A= not defined $configfileB= $cfmeta->has_default
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 16 | ▶ | if (ref $configfile eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 9 | $configfile = $configfile->($class); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 16 | my $init_arg = $cfmeta->init_arg; | |||||||||||||||||||||||||||
| 54 | 16 | ▶ | $opts{$init_arg} = $configfile if defined $configfile and defined $init_arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $configfile and defined $init_arg
MC/DC50%1/2
defined $configfile and defined $init_arg
defined $configfile
defined $init_arg
Truth table66%2/3
A and B
A= defined $configfileB= defined $init_arg
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | ▶ | if (defined $configfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 12 | my $hash = $class->get_config_from_file($configfile); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 12 | ▶ | croak "get_config_from_file($configfile) did not return a hash (got $hash)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | unless ref $hash eq 'HASH'; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 12 | %opts = (%$hash, %opts); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 16 | $class->new(%opts); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | ▶ | no Moose::Role; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||