File Coverage

lib/Smartcat/App/Config.pm
Criterion Covered Total %
statement 51 54 94.4
branch 6 8 75.0
condition 5 6 83.3
subroutine 13 14 92.8
pod 1 5 20.0
total 76 87 87.3


line stmt bran cond sub pod time code
1 3     3   758 use strict;
  3         6  
  3         93  
2 3     3   14 use warnings;
  3         15  
  3         126  
3              
4             package Smartcat::App::Config;
5 3     3   15 use base ( "Class::Accessor", "Class::Data::Inheritable" );
  3         6  
  3         920  
6              
7 3     3   5309 use Config::Tiny;
  3         3346  
  3         95  
8 3     3   20 use File::Basename;
  3         5  
  3         232  
9 3     3   22 use File::Spec::Functions qw(catfile);
  3         6  
  3         129  
10 3     3   1669 use File::HomeDir;
  3         17542  
  3         159  
11              
12 3     3   746 use Data::Dumper;
  3         6917  
  3         1558  
13              
14             __PACKAGE__->mk_classdata( 'attribute_map' => {} );
15              
16             sub new {
17 8     8 1 17 my $class = shift @_;
18              
19 8         17 my $self = bless( {}, $class );
20              
21 8         16 return $self;
22             }
23              
24             sub validate_log {
25 8     8 0 21 my ( $self, $value ) = @_;
26              
27 8 100 100     318 die
28             "ConfigError: 'log' parent directory, which is set to '$value', does not point to a valid directory"
29             if defined $value && !-d dirname($value);
30              
31 7         59 return 1;
32             }
33              
34             sub get_config_file {
35 2     2   14 my $config_dir =
36             File::HomeDir->my_dist_config( "Smartcat-App", { create => 1 } );
37 2         680 return catfile( $config_dir, 'config' );
38             }
39              
40             sub load {
41 8     8 0 3901 my $self = shift @_;
42 8 50       40 $self = $self->new unless ref $self;
43              
44 8         27 my $config_file = $self->get_config_file;
45 8 100       202 if ( -e $config_file ) {
46 5         46 $self->{instance} = Config::Tiny->read( $config_file, "utf8" );
47             }
48             else {
49 3         30 $self->{instance} = Config::Tiny->new;
50             }
51              
52 8         2115 foreach my $attribute ( keys %{ $self->attribute_map } ) {
  8         35  
53 32         423 my $args_key = $self->attribute_map->{$attribute};
54 32         246 my $validate_attribute = "validate_$attribute";
55 32         57 my $value = $self->{instance}->{_}->{$args_key};
56 32 50 66     233 $self->$attribute($value)
57             if !defined $self->can($validate_attribute)
58             || $self->$validate_attribute($value);
59             }
60              
61 7         115 return $self;
62             }
63              
64             sub save {
65 1     1 0 2284 my $self = shift @_;
66 1         2 foreach my $attribute ( keys %{ $self->attribute_map } ) {
  1         3  
67 4         42 my $args_key = $self->attribute_map->{$attribute};
68 4         30 $self->{instance}->{_}->{$args_key} = $self->$attribute;
69             }
70 1         11 $self->{instance}->write( $self->get_config_file, "utf8" );
71             }
72              
73             sub cat {
74 0     0 0   my $self = shift @_;
75 0           my $config_file = $self->get_config_file;
76 0           print `cat $config_file\n`;
77             }
78              
79             __PACKAGE__->attribute_map(
80             {
81             'username' => 'token_id',
82             'password' => 'token',
83             'log' => 'log',
84             'base_url' => 'base_url'
85             }
86             );
87              
88             __PACKAGE__->mk_accessors( keys %{ __PACKAGE__->attribute_map } );
89              
90             1;