File Coverage

blib/lib/Mo/utils/common.pm
Criterion Covered Total %
statement 34 35 97.1
branch 11 12 91.6
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Mo::utils::common;
2              
3 3     3   162253 use base qw(Exporter);
  3         6  
  3         423  
4 3     3   29 use strict;
  3         6  
  3         87  
5 3     3   13 use warnings;
  3         6  
  3         164  
6              
7 3     3   1575 use Error::Pure qw(err);
  3         31022  
  3         77  
8 3     3   314 use Readonly;
  3         7  
  3         215  
9 3     3   21 use Scalar::Util qw(blessed);
  3         9  
  3         1376  
10              
11             Readonly::Array our @EXPORT_OK => qw(check_object);
12              
13             our $VERSION = 0.01;
14              
15             sub check_object {
16 5     5 1 365150 my ($value, $class, $message, $message_params_ar) = @_;
17              
18 5 100       27 if (! blessed($value)) {
19 3         8 my $err_message = _error_message($message, $message_params_ar);
20 3 100       19 err $err_message,
    100          
21              
22             # Only, if value is scalar.
23             (ref $value eq '') ? (
24             'Value', $value,
25             ) : (),
26              
27             # Only if value is reference.
28             (ref $value ne '') ? (
29             'Reference', (ref $value),
30             ) : (),
31             }
32              
33 2 100       8 if (! $value->isa($class)) {
34 1         43 my $err_message = _error_message($message, $message_params_ar);
35 1         4 err $err_message,
36             'Reference', (ref $value),
37             ;
38             }
39              
40 1         25 return;
41             }
42              
43             sub _error_message {
44 4     4   10 my ($message, $message_params_ar) = @_;
45              
46 4 50 66     18 if (defined $message_params_ar && ref $message_params_ar ne 'ARRAY') {
47 0         0 err "Bad definition of $message_params_ar.",
48             'Message params array refence', $message_params_ar,
49             ;
50             }
51              
52 4         7 my $err_message;
53 4 100       12 if (defined $message_params_ar) {
54 1         2 $err_message = sprintf $message, @{$message_params_ar};
  1         5  
55             } else {
56 3         5 $err_message = $message;
57             }
58              
59 4         10 return $err_message;
60              
61             }
62              
63             1;
64              
65             __END__
66              
67             =pod
68              
69             =encoding utf8
70              
71             =head1 NAME
72              
73             Mo::utils::common - Common subroutines for Mo::utils framework.
74              
75             =head1 SYNOPSIS
76              
77             use Mo::utils::common qw(check_object);
78              
79             check_object($value, $class, $message, $message_params_ar);
80              
81             =head1 SUBROUTINES
82              
83             =head2 C<check_object>
84              
85             check_object($value, $class, $message, $message_params_ar);
86              
87             Check C<$value> which is instance of C<$class> or not.
88              
89             Put error (defined by C<$message> and C<$message_params_ar>) if check isn't ok.
90              
91             Returns undef.
92              
93             =head1 ERRORS
94              
95             check_object():
96             I<Create own errors from C<sprintf $message, @{$message_params_ar}>.>
97             Bad definition of $message_params_ar.
98             Message params array reference: %s
99              
100             =head1 EXAMPLE1
101              
102             =for comment filename=check_object_scalar.pl
103              
104             use strict;
105             use warnings;
106              
107             use Error::Pure;
108             use Mo::utils::common qw(check_object);
109              
110             $Error::Pure::TYPE = 'AllError';
111              
112             # Value to check.
113             my $value = 'Foo';
114              
115             # Check.
116             check_object($value, 'Foo', 'Error: This is not a instance of %s.', ['Foo']);
117              
118             # Output:
119             # ERROR: Error: This is not a instance of Foo.
120             # Value: Foo
121             # Mo::utils::common err ../common.pm 20
122             # main Mo::utils::common::check_object ../check_object_scalar.pl 12
123              
124             =head1 EXAMPLE2
125              
126             =for comment filename=check_object_object.pl
127              
128             use strict;
129             use warnings;
130              
131             use Error::Pure;
132             use Mo::utils::common qw(check_object);
133             use Test::MockObject;
134              
135             $Error::Pure::TYPE = 'AllError';
136              
137             # Value to check.
138             my $value = Test::MockObject->new;
139              
140             # Check.
141             check_object($value, 'Foo', 'Error: This is not a instance of %s.', ['Foo']);
142              
143             # Output:
144             # ERROR: Error: This is not a instance of Foo.
145             # Reference: Reference: Test::MockObject
146             # Mo::utils::common err ../common.pm 35
147             # main Mo::utils::common::check_object ../check_object_scalar.pl 13
148              
149             =head1 DEPENDENCIES
150              
151             L<Error::Pure>,
152             L<Exporter>,
153             L<Readonly>,
154             L<Scalar::Util>.
155              
156             =head1 REPOSITORY
157              
158             L<https://github.com/michal-josef-spacek/Mo-utils-common>
159              
160             =head1 AUTHOR
161              
162             Michal Josef Špaček L<mailto:skim@cpan.org>
163              
164             L<http://skim.cz>
165              
166             =head1 LICENSE AND COPYRIGHT
167              
168             © 2025-2026 Michal Josef Špaček
169              
170             BSD 2-Clause License
171              
172             =head1 VERSION
173              
174             0.01
175              
176             =cut