File Coverage

blib/lib/Data/Message/Simple.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Data::Message::Simple;
2              
3 6     6   218027 use strict;
  6         14  
  6         230  
4 6     6   31 use warnings;
  6         12  
  6         373  
5              
6 6     6   3120 use Mo qw(build is);
  6         4188  
  6         33  
7 6     6   14974 use Mo::utils 0.15 qw(check_length check_required check_strings);
  6         152122  
  6         157  
8 6     6   4980 use Mo::utils::Language 0.05 qw(check_language_639_1);
  6         1787370  
  6         190  
9 6     6   510 use Readonly;
  6         13  
  6         1483  
10              
11             Readonly::Array our @TYPES => qw(info error);
12              
13             our $VERSION = 0.05;
14              
15             has lang => (
16             is => 'ro',
17             );
18              
19             has text => (
20             is => 'ro',
21             );
22              
23             has type => (
24             is => 'ro',
25             );
26              
27             sub BUILD {
28 12     12 0 1421737 my $self = shift;
29              
30             # Check lang.
31 12         74 check_language_639_1($self, 'lang');
32              
33             # Check text.
34 11         1207 check_required($self, 'text');
35 10         96 check_length($self, 'text', 4096);
36              
37             # Check message type.
38 9 100       162 if (! defined $self->{'type'}) {
39 5         17 $self->{'type'} = 'info';
40             }
41 9         40 check_strings($self, 'type', \@TYPES);
42              
43 8         368 return;
44             }
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding utf8
53              
54             =head1 NAME
55              
56             Data::Message::Simple - Data object for simple message.
57              
58             =head1 SYNOPSIS
59              
60             use Data::Message::Simple;
61              
62             my $obj = Data::Message::Simple->new(%params);
63             my $lang = $obj->lang;
64             my $text = $obj->text;
65             my $type = $obj->type;
66              
67             =head1 METHODS
68              
69             =head2 C<new>
70              
71             my $obj = Data::Message::Simple->new(%params);
72              
73             Constructor.
74              
75             =over 8
76              
77             =item * C<lang>
78              
79             Message language.
80             It's optional.
81             If defined, possible values are ISO 639-1 language codes.
82              
83             Default value is undef.
84              
85             =item * C<text>
86              
87             Message text.
88             Maximum length of text is 4096 characters.
89             It's required.
90              
91             =item * C<type>
92              
93             Message type.
94             Possible value are 'error' and 'info'.
95             It's required.
96             Default value is 'info'.
97              
98             =back
99              
100             Returns instance of object.
101              
102             =head2 C<lang>
103              
104             my $lang = $obj->lang;
105              
106             Get ISO 639-1 language code of text.
107              
108             Returns string.
109              
110             =head2 C<text>
111              
112             my $text = $obj->text;
113              
114             Get message text.
115              
116             Returns string.
117              
118             =head2 C<type>
119              
120             my $type = $obj->type;
121              
122             Get message type.
123              
124             Returns string.
125              
126             =head1 ERRORS
127              
128             new():
129             From Mo::utils:
130             Parameter 'text' has length greater than '4096'.
131             Value: %s
132             Parameter 'text' is required.
133             Value: %s
134             Parameter 'type' must be one of defined strings.
135             String: %s
136             Possible strings: %s
137             From Mo::utils::Language:
138             Parameter 'lang' doesn't contain valid ISO 639-1 code.
139             Codeset: %s
140             Value: %s
141              
142             =head1 EXAMPLE
143              
144             =for comment filename=create_and_print_message.pl
145              
146             use strict;
147             use warnings;
148              
149             use Data::Message::Simple;
150              
151             my $obj = Data::Message::Simple->new(
152             'lang' => 'en',
153             'text' => 'This is text message.',
154             );
155              
156             # Print out.
157             print 'Message type: '.$obj->type."\n";
158             print 'ISO 639-1 language code: '.$obj->lang."\n";
159             print 'Text: '.$obj->text."\n";
160              
161             # Output:
162             # Message type: info
163             # ISO 639-1 language code: en
164             # Text: This is text message.
165              
166             =head1 DEPENDENCIES
167              
168             L<Mo>,
169             L<Mo::utils>,
170             L<Mo::utils::Language>,
171             L<Readonly>.
172              
173             =head1 REPOSITORY
174              
175             L<https://github.com/michal-josef-spacek/Data-Message-Simple>
176              
177             =head1 AUTHOR
178              
179             Michal Josef Špaček L<mailto:skim@cpan.org>
180              
181             L<http://skim.cz>
182              
183             =head1 LICENSE AND COPYRIGHT
184              
185             © 2023-2025 Michal Josef Špaček
186              
187             BSD 2-Clause License
188              
189             =head1 VERSION
190              
191             0.05
192              
193             =cut