File Coverage

blib/lib/Dist/Iller/Config.pm
Criterion Covered Total %
statement 59 60 98.3
branch 1 2 50.0
condition n/a
subroutine 19 20 95.0
pod 0 2 0.0
total 79 84 94.0


line stmt bran cond sub pod time code
1 1     1   37559 use 5.14.0;
  1         4  
2 1     1   6 use strict;
  1         3  
  1         21  
3 1     1   5 use warnings;
  1         7  
  1         70  
4              
5             package Dist::Iller::Config;
6              
7             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8             # ABSTRACT: Role for Dist::Iller configs
9             our $VERSION = '0.1411';
10              
11 1     1   10 use Moose::Role;
  1         4  
  1         15  
12 1     1   5569 use MooseX::AttributeShortcuts;
  1         2  
  1         14  
13 1     1   32152 use namespace::autoclean;
  1         3  
  1         7  
14 1     1   86 use Module::Load qw/load/;
  1         3  
  1         9  
15 1     1   94 use Types::Standard qw/Bool Str Maybe InstanceOf/;
  1         3  
  1         8  
16 1     1   1003 use YAML::Tiny;
  1         3  
  1         76  
17 1     1   10 use Path::Tiny;
  1         2  
  1         56  
18 1     1   7 use Try::Tiny;
  1         2  
  1         52  
19 1     1   5 use Carp qw/croak/;
  1         15  
  1         56  
20 1     1   8 use File::ShareDir 'dist_dir';
  1         2  
  1         69  
21 1     1   12 use String::CamelCase qw/camelize/;
  1         2  
  1         617  
22              
23             requires qw/filepath/;
24              
25             has main_module => (
26             is => 'ro',
27             isa => Str,
28             lazy => 1,
29             traits => ['Documented'],
30             default => sub { shift->meta->name },
31             documentation => q{Override this attribute when there's more than one config in a distribution. It uses the main_module's sharedir location for the config files.},
32             documentation_default => 'The package name',
33             );
34             has distribution_name => (
35             is => 'ro',
36             isa => Str,
37             lazy => 1,
38             predicate => 1,
39             default => sub { undef },
40             traits => ['Documented'],
41             documentation_order => 0,
42             );
43             has global => (
44             is => 'ro',
45             isa => Maybe[InstanceOf['Dist::Iller::DocType::Global']],
46             );
47              
48              
49             sub config_location {
50 3     3 0 5 my $self = shift;
51 3         108 my $package = $self->main_module;
52 3         22 $package =~ s{::}{-}g;
53              
54 3         21 my $dir = path('.');
55              
56             try {
57 3     3   216 $dir = path(dist_dir($package));
58             }
59 3     3   209 finally { };
60              
61 3         54 return $dir->child($self->filepath);
62             }
63              
64             sub get_yaml_for {
65 3     3 0 8 my $self = shift;
66 3         6 my $doctype = shift;
67              
68 3         13 my $fullyaml = YAML::Tiny->read($self->config_location->absolute->stringify);
69              
70 3         19211 my $yaml = (grep { $_->{'doctype'} eq $doctype } @{ $fullyaml })[0];
  9         26  
  3         9  
71 3 50       13 return if !defined $yaml;
72              
73 3         17 my $doctype_class = sprintf 'Dist::Iller::DocType::%s', camelize($yaml->{'doctype'});
74             try {
75 3     3   153 load $doctype_class;
76             }
77             catch {
78 0     0   0 croak "Can't load $doctype_class: $_";
79 3         76 };
80              
81 3         387 return $doctype_class->new(config_obj => $self)->parse($yaml)->to_yaml;
82              
83             }
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =encoding UTF-8
92              
93             =head1 NAME
94              
95             Dist::Iller::Config - Role for Dist::Iller configs
96              
97             =head1 VERSION
98              
99             Version 0.1411, released 2020-01-01.
100              
101             =head1 SOURCE
102              
103             L<https://github.com/Csson/p5-Dist-Iller>
104              
105             =head1 HOMEPAGE
106              
107             L<https://metacpan.org/release/Dist-Iller>
108              
109             =head1 AUTHOR
110              
111             Erik Carlsson <info@code301.com>
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is copyright (c) 2021 by Erik Carlsson.
116              
117             This is free software; you can redistribute it and/or modify it under
118             the same terms as the Perl 5 programming language system itself.
119              
120             =cut