File Coverage

blib/lib/MooseX/App/Plugin/Config/Meta/Class.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 10 100.0
condition 6 7 85.7
subroutine 5 5 100.0
pod 0 1 0.0
total 53 55 96.3


line stmt bran cond sub pod time code
1             # ============================================================================
2             package MooseX::App::Plugin::Config::Meta::Class;
3             # ============================================================================
4              
5 6     6   5305 use 5.010;
  6         27  
6 6     6   44 use utf8;
  6         18  
  6         53  
7              
8 6     6   174 use namespace::autoclean;
  6         14  
  6         51  
9 6     6   576 use Moose::Role;
  6         20  
  6         54  
10              
11             around 'command_proto' => sub {
12             my ($orig,$self,$metaclass) = @_;
13              
14             my ($result,$errors) = $self->$orig($metaclass);
15             delete $result->{config}
16             unless defined $result->{config};
17              
18             return $self->proto_config($metaclass,$result,$errors);
19             };
20              
21             sub proto_config {
22 20     20 0 82 my ($self,$metaclass,$result,$errors) = @_;
23              
24             # Check if we have a config
25             return ($result,$errors)
26 20 100       128 unless defined $result->{config};
27              
28             # Read config
29 4         19 my $config_file = $result->{config};
30              
31 4 100       168 unless (-e $config_file) {
32 1         18 push(@{$errors},
  1         25  
33             $self->command_message(
34             header => "Could not find config file '".$config_file."'",
35             type => "error",
36             ),
37             );
38 1         11 return ($result,$errors);
39             }
40              
41 3         77 my $configs = Config::Any->load_files({
42             files => [ $config_file ],
43             use_ext => 1,
44             });
45              
46 3         40492 my $command_name = $self->command_class_to_command($metaclass->name);
47              
48 3         14 my ($config_data) = values %{$configs->[0]};
  3         29  
49              
50             # Merge
51 3   50     26 $config_data->{global} ||= {};
52 3   100     44 $config_data->{$command_name} ||= {};
53              
54             # Set config data
55 3         22 $result->{config} = $result->{config};
56 3         30 $result->{_config_data} = $config_data;
57              
58             # Lopp all attributes
59              
60 3         78 foreach my $attribute ($self->command_usage_attributes($metaclass,'all')) {
61 14         108 my $attribute_name = $attribute->name;
62             next
63 14 100 100     111 if $attribute_name eq 'config' || $attribute_name eq 'help_flag';
64             $result->{$attribute_name} = $config_data->{global}{$attribute_name}
65 8 100       36 if defined $config_data->{global}{$attribute_name};
66             $result->{$attribute_name} = $config_data->{$command_name}{$attribute_name}
67 8 100       47 if defined $config_data->{$command_name}{$attribute_name};
68             }
69              
70 3         223 return ($result,$errors);
71             };
72              
73             1;