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   82576 use strict;
  6         44  
  6         174  
4 6     6   31 use warnings;
  6         12  
  6         272  
5              
6 6     6   2705 use Mo qw(build is);
  6         3250  
  6         33  
7 6     6   11848 use Mo::utils qw(check_length check_required check_strings);
  6         93319  
  6         110  
8 6     6   3305 use Mo::utils::Language qw(check_language);
  6         1308788  
  6         170  
9 6     6   473 use Readonly;
  6         14  
  6         1282  
10              
11             Readonly::Array our @TYPES => qw(info error);
12              
13             our $VERSION = 0.02;
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 10475 my $self = shift;
29              
30             # Check lang.
31 12         54 check_language($self, 'lang');
32              
33             # Check text.
34 11         843 check_required($self, 'text');
35 10         94 check_length($self, 'text', 4096);
36              
37             # Check message type.
38 9 100       147 if (! defined $self->{'type'}) {
39 5         21 $self->{'type'} = 'info';
40             }
41 9         37 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 $lane = $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             Parameter 'text' is required.
132             Parameter 'type' must be one of defined strings.
133             From Mo::utils::Language:
134             Language code 'xx' isn't ISO 639-1 code.
135              
136             =head1 EXAMPLE
137              
138             =for comment filename=create_and_print_message.pl
139              
140             use strict;
141             use warnings;
142              
143             use Data::Message::Simple;
144              
145             my $obj = Data::Message::Simple->new(
146             'lang' => 'en',
147             'text' => 'This is text message.',
148             );
149              
150             # Print out.
151             print 'Message type: '.$obj->type."\n";
152             print 'ISO 639-1 language code: '.$obj->lang."\n";
153             print 'Text: '.$obj->text."\n";
154              
155             # Output:
156             # Message type: info
157             # ISO 639-1 language code: en
158             # Text: This is text message.
159              
160             =head1 DEPENDENCIES
161              
162             L<Mo>,
163             L<Mo::utils>,
164             L<Mo::utils::Language>,
165             L<Readonly>.
166              
167             =head1 REPOSITORY
168              
169             L<https://github.com/michal-josef-spacek/Data-Message-Simple>
170              
171             =head1 AUTHOR
172              
173             Michal Josef Špaček L<mailto:skim@cpan.org>
174              
175             L<http://skim.cz>
176              
177             =head1 LICENSE AND COPYRIGHT
178              
179             © 2023 Michal Josef Špaček
180              
181             BSD 2-Clause License
182              
183             =head1 VERSION
184              
185             0.02
186              
187             =cut