File Coverage

blib/lib/Mo/utils/Array.pm
Criterion Covered Total %
statement 72 72 100.0
branch 24 24 100.0
condition 6 6 100.0
subroutine 14 14 100.0
pod 5 5 100.0
total 121 121 100.0


line stmt bran cond sub pod time code
1             package Mo::utils::Array;
2              
3 7     7   160992 use base qw(Exporter);
  7         13  
  7         757  
4 7     7   59 use strict;
  7         10  
  7         169  
5 7     7   21 use warnings;
  7         17  
  7         287  
6              
7 7     7   2731 use Error::Pure qw(err);
  7         23185  
  7         122  
8 7     7   458 use List::Util 1.33 qw(none);
  7         115  
  7         774  
9 7     7   3000 use Mo::utils::common qw(check_object);
  7         4682  
  7         210  
10 7     7   410 use Readonly;
  7         10  
  7         244  
11 7     7   35 use Scalar::Util qw(blessed);
  7         22  
  7         5594  
12              
13             Readonly::Array our @EXPORT_OK => qw(check_array check_array_items check_array_object
14             check_array_required check_array_strings);
15              
16             our $VERSION = 0.05;
17              
18             sub check_array {
19 27     27 1 249916 my ($self, $key) = @_;
20              
21 27 100       66 if (! exists $self->{$key}) {
22 1         2 return;
23             }
24              
25 26 100       65 if (ref $self->{$key} ne 'ARRAY') {
26 10         23 my $ref = ref $self->{$key};
27             err "Parameter '".$key."' must be a array.",
28 10 100       65 'Value', $self->{$key},
29             'Reference', ($ref eq '' ? 'SCALAR' : $ref),
30             ;
31             }
32              
33 16         26 return;
34             }
35              
36             sub check_array_items {
37 5     5 1 239975 my ($self, $key, $max_items) = @_;
38              
39 5 100       15 if (! exists $self->{$key}) {
40 1         3 return;
41             }
42              
43 4         11 check_array($self, $key);
44              
45 3 100       3 if (@{$self->{$key}} > $max_items) {
  3         8  
46             err "Parameter '".$key."' has more items than expected.",
47             'Maximum items', $max_items,
48 1         2 'Number of items', (scalar @{$self->{$key}}),
  1         4  
49             ;
50             }
51              
52 2         5 return;
53             }
54              
55             sub check_array_object {
56 6     6 1 252315 my ($self, $key, $class) = @_;
57              
58 6 100       19 if (! exists $self->{$key}) {
59 1         3 return;
60             }
61              
62 5         14 check_array($self, $key);
63              
64 4         5 foreach my $obj (@{$self->{$key}}) {
  4         9  
65 4         12 check_object($obj, $class,
66             "Parameter '%s' with array must contain '%s' objects.",
67             [$key, $class],
68             );
69             }
70              
71 1         9 return;
72             }
73              
74             sub check_array_required {
75 5     5 1 221160 my ($self, $key) = @_;
76              
77 5 100       13 if (! exists $self->{$key}) {
78 1         4 err "Parameter '$key' is required.";
79             }
80              
81 4         10 check_array($self, $key);
82              
83 2 100       2 if (! @{$self->{$key}}) {
  2         6  
84 1         5 err "Parameter '".$key."' with array must have at least one item.";
85             }
86              
87 1         3 return;
88             }
89              
90             sub check_array_strings {
91 11     11 1 232578 my ($self, $key, $strings_ar) = @_;
92              
93 11 100       31 if (! exists $self->{$key}) {
94 1         3 return;
95             }
96              
97 10 100 100     45 if (defined $strings_ar && ref $strings_ar ne 'ARRAY') {
98 1         6 err "Parameter '$key' must have right string definition.";
99             }
100              
101 9         25 check_array($self, $key);
102              
103 6         7 foreach my $value (@{$self->{$key}}) {
  6         14  
104 6 100       35 if (ref $value ne '') {
105 1         6 err "Parameter '$key' must contain a list of strings.",
106             'Value', $value,
107             'Reference', (ref $value),
108             ;
109             }
110 5 100 100 6   21 if (defined $strings_ar && none { $value eq $_ } @{$strings_ar}) {
  6         49  
  4         15  
111             err "Parameter '$key' must be one of the defined strings.",
112             'Value', $value,
113 1         11 'Possible strings', "'".(join "', '", @{$strings_ar})."'",
  1         2  
114             ;
115             }
116             }
117              
118 4         17 return;
119             }
120              
121             1;
122              
123             __END__