File Coverage

blib/lib/Daje/Config.pm
Criterion Covered Total %
statement 26 43 60.4
branch 1 4 25.0
condition 1 3 33.3
subroutine 5 7 71.4
pod 1 2 50.0
total 34 59 57.6


line stmt bran cond sub pod time code
1             package Daje::Config;
2 2     2   512233 use Mojo::Base -base, -signatures;
  2         29911  
  2         16  
3              
4              
5 2     2   10362 use Mojo::JSON qw {decode_json from_json};
  2         526315  
  2         194  
6 2     2   1409 use Mojo::File;
  2         90775  
  2         3437  
7              
8             # NAME
9             # ====
10             #
11             # Daje::Config - Loads the JSON based configs and put them in a hash
12             #
13             # SYNOPSIS
14             # ========
15             #
16             # use Daje::Config;
17             #
18             # # Single file
19             # my $config = Daje::Config->new(
20             # path => "path",
21             # )->load($filename);
22             #
23             # # All files in path
24             # my $config = Daje::Config->new(
25             # path => "path",
26             # )->load();
27             #
28             # # All files in path
29             # my $file_collection = Daje::Config->new(
30             # path => "path",
31             # )->load_list();
32             #
33             # DESCRIPTION
34             # ===========
35             #
36             # Daje::Config is loading workflows from JSON files in a set folder
37             #
38             # LICENSE
39             # =======
40             #
41             # Copyright (C) janeskil1525.
42             #
43             # This library is free software; you can redistribute it and/or modify
44             # it under the same terms as Perl itself.
45             #
46             # AUTHOR
47             # ======
48             #
49             # janeskil1525 Ejaneskil1525@gmail.comE
50             #
51              
52             our $VERSION = "0.06";
53              
54             has 'path';
55             has 'type' => "config";
56              
57             # Load all workflows in the given path
58 1     1 1 259570 sub load($self, $filename = "") {
  1         3  
  1         7  
  1         2  
59 1         3 my $config;
60 1 50 33     9 unless (defined $filename and length($filename) > 0) {
61 1         7 my $collection = $self->load_list();
62 0     0   0 $collection->each(sub($file, $num) {
  0         0  
  0         0  
  0         0  
63 0         0 $config = $self->_load_config($config, $file);
64 1         12 });
65             } else {
66 0         0 my $path = $self->path();
67 0 0       0 $path .= "/" unless (substr($path, -1) eq "/");
68 0         0 $config = $self->_load_config($config, $path . $filename);
69             }
70 1         26 return $config;
71             }
72              
73 0     0   0 sub _load_config($self, $config, $file) {
  0         0  
  0         0  
  0         0  
  0         0  
74 0         0 my $path = Mojo::File->new($file);
75 0         0 my $tag = substr($path->basename(), 0, index($path->basename(), '.json'));
76 0         0 $config->{$tag} = from_json($path->slurp())->{$self->type()};
77              
78 0         0 return $config;
79             }
80              
81             # List of workflows in path (for internal use)
82 1     1 0 2 sub load_list($self) {
  1         4  
  1         2  
83 1         2 my $collection;
84 1         2 eval {
85 1         6 my $path = Mojo::File->new($self->path());
86 1         34 $collection = $path->list();
87             };
88              
89 1         248 return $collection;
90             }
91              
92             1;
93             __END__