File Coverage

lib/Perl/Critic/MergeProfile.pm
Criterion Covered Total %
statement 66 66 100.0
branch 22 22 100.0
condition 12 12 100.0
subroutine 12 12 100.0
pod 5 5 100.0
total 117 117 100.0


line stmt bran cond sub pod time code
1             package Perl::Critic::MergeProfile;
2              
3 7     7   418473 use 5.006;
  7         72  
4 7     7   29 use strict;
  7         12  
  7         144  
5 7     7   31 use warnings;
  7         12  
  7         297  
6              
7             our $VERSION = '0.003';
8              
9 7     7   34 use Carp ();
  7         10  
  7         130  
10 7     7   2313 use Config::Tiny ();
  7         4932  
  7         104  
11 7     7   33 use Scalar::Util ();
  7         11  
  7         3462  
12              
13             sub new {
14 13     13 1 6808 my ($class) = @_;
15              
16 13         32 my $self = bless {}, $class;
17              
18 13         35 return $self;
19             }
20              
21             sub read {
22 2     2 1 353 my $self = shift;
23              
24 2         10 my $config = Config::Tiny->read(@_);
25              
26 2 100       417 Carp::croak Config::Tiny->errstr() if !defined $config;
27              
28 1         4 $self->_merge($config);
29              
30 1         8 return $self;
31             }
32              
33             sub read_string {
34 8     8 1 326 my $self = shift;
35              
36 8         38 my $config = Config::Tiny->read_string(@_);
37              
38 8 100       561 Carp::croak Config::Tiny->errstr() if !defined $config;
39              
40 7         22 $self->_merge($config);
41              
42 7         29 return $self;
43             }
44              
45             sub _merge {
46 15     15   1573 my ( $self, $config ) = @_;
47              
48 15 100       46 if ( !exists $self->{_config} ) {
49 6         15 $self->{_config} = $config;
50 6         19 return;
51             }
52              
53 9         17 for my $key ( grep { !m{ ^ - }xsm } keys %{$config} ) {
  10         40  
  9         39  
54 8 100       210 Carp::croak "$key is enabled and disabled in the same profile" if exists $config->{"-$key"};
55             }
56              
57             KEY:
58 8         17 for my $key ( keys %{$config} ) {
  8         24  
59 8 100       23 if ( $key =~ m { ^ - (.+ ) }xsm ) {
60 1         4 my $policy = $1;
61 1         5 delete $self->{_config}{$policy};
62 1         3 $self->{_config}{$key} = {};
63 1         4 next KEY;
64             }
65              
66 7 100       22 if ( $key eq '_' ) {
67 4 100       14 if ( !exists $self->{_config}{'_'} ) {
68 1         2 $self->{_config}{'_'} = {};
69             }
70              
71 4         8 %{ $self->{_config}{'_'} } = ( %{ $self->{_config}{'_'} }, %{ $config->{'_'} } );
  4         15  
  4         13  
  4         10  
72 4         43 next KEY;
73             }
74              
75 3         9 delete $self->{_config}{"-$key"};
76 3         9 $self->{_config}{$key} = $config->{$key};
77             }
78              
79 8         39 return;
80             }
81              
82             sub write {
83 6     6 1 1609 my $self = shift;
84              
85 6 100 100     317 Carp::croak 'No policy exists to write' if !exists $self->{_config} || !Scalar::Util::blessed( $self->{_config} ) || !$self->{_config}->isa('Config::Tiny');
      100        
86              
87 3         10 my $rc = $self->{_config}->write(@_);
88              
89 3 100       443 Carp::croak Config::Tiny->errstr() if !$rc;
90              
91 2         11 return $rc;
92             }
93              
94             sub write_string {
95 6     6 1 1120 my $self = shift;
96              
97 6 100 100     277 Carp::croak 'No policy exists to write' if !exists $self->{_config} || !Scalar::Util::blessed( $self->{_config} ) || !$self->{_config}->isa('Config::Tiny');
      100        
98              
99 3         9 my $string = $self->{_config}->write_string(@_);
100              
101 3 100       156 Carp::croak Config::Tiny->errstr() if !defined $string;
102              
103 2         10 return $string;
104             }
105              
106             1;
107              
108             __END__