File Coverage

blib/lib/Role/HasMessage/Errf.pm
Criterion Covered Total %
statement 15 16 93.7
branch n/a
condition n/a
subroutine 6 7 85.7
pod n/a
total 21 23 91.3


line stmt bran cond sub pod time code
1             package Role::HasMessage::Errf 0.007;
2 1     1   4444 use MooseX::Role::Parameterized;
  1         78366  
  1         3  
3             # ABSTRACT: a thing with a String::Errf-powered message
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod In your class...
8             #pod
9             #pod package Errfy;
10             #pod use Moose;
11             #pod
12             #pod with 'Role::HasMessage::Errf';
13             #pod
14             #pod has payload => (
15             #pod is => 'ro',
16             #pod isa => 'HashRef',
17             #pod required => 1,
18             #pod );
19             #pod
20             #pod Then...
21             #pod
22             #pod my $thing = Errfy->new({
23             #pod message => "%{error_count;error}n encountered at %{when}t",
24             #pod payload => {
25             #pod error_count => 2,
26             #pod when => time,
27             #pod },
28             #pod });
29             #pod
30             #pod # prints: 2 errors encountered at 2010-10-20 19:23:42
31             #pod print $thing->message;
32             #pod
33             #pod =head1 DESCRIPTION
34             #pod
35             #pod Role::HasMessage::Errf is an implementation of L<Role::HasMessage> that uses
36             #pod L<String::Errf> to format C<sprintf>-like message strings. It adds a
37             #pod C<message_fmt> attribute, initialized by the C<message> argument. The value
38             #pod should be a String::Errf format string.
39             #pod
40             #pod When the provided C<message> method is called, it will fill in the format
41             #pod string with the hashref returned by calling the C<payload> method, which I<must
42             #pod be implemented by the including class>.
43             #pod
44             #pod Role::HasMessage::Errf is a L<parameterized role|MooseX::Role::Parameterized>.
45             #pod The C<default> parameter lets you set a default format string or callback. The
46             #pod C<lazy> parameter sets whether or not the C<message_fmt> attribute is lazy.
47             #pod Setting it lazy will require that a default is provided.
48             #pod
49             #pod =cut
50              
51 1     1   32707 use String::Errf qw(errf);
  1         234907  
  1         1108  
52 1     1   371 use Try::Tiny;
  1         3  
  1         64  
53              
54 1     1   6 use namespace::clean -except => 'meta';
  1         1  
  1         9  
55              
56             parameter default => (
57             isa => 'CodeRef|Str',
58             );
59              
60             parameter lazy => (
61             isa => 'Bool',
62             default => 0,
63             );
64              
65             role {
66             my $p = shift;
67              
68             requires 'payload';
69              
70             my $msg_default = $p->default;
71             has message_fmt => (
72             is => 'ro',
73             isa => 'Str',
74             lazy => $p->lazy,
75             required => 1,
76             init_arg => 'message',
77             (defined $msg_default ? (default => $msg_default) : ()),
78             );
79              
80             # The problem with putting this in a cached attribute is that we need to
81             # clear it any time the payload changes. We can do that by making the
82             # Payload trait add a trigger to clear the message, but I haven't done so
83             # yet. -- rjbs, 2010-10-16
84             # has message => (
85             # is => 'ro',
86             # lazy => 1,
87             # init_arg => undef,
88             # default => sub { __stringf($_[0]->message_fmt, $_[0]->data) },
89             # );
90              
91             method message => sub {
92 1     1   3551 my ($self) = @_;
93             return try {
94 1     1   64 errf($self->message_fmt, $self->payload)
95             } catch {
96 0     0     sprintf '%s (error during formatting)', $self->message_fmt;
97             }
98 1         8 };
99              
100             with 'Role::HasMessage';
101             };
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Role::HasMessage::Errf - a thing with a String::Errf-powered message
114              
115             =head1 VERSION
116              
117             version 0.007
118              
119             =head1 SYNOPSIS
120              
121             In your class...
122              
123             package Errfy;
124             use Moose;
125              
126             with 'Role::HasMessage::Errf';
127              
128             has payload => (
129             is => 'ro',
130             isa => 'HashRef',
131             required => 1,
132             );
133              
134             Then...
135              
136             my $thing = Errfy->new({
137             message => "%{error_count;error}n encountered at %{when}t",
138             payload => {
139             error_count => 2,
140             when => time,
141             },
142             });
143              
144             # prints: 2 errors encountered at 2010-10-20 19:23:42
145             print $thing->message;
146              
147             =head1 DESCRIPTION
148              
149             Role::HasMessage::Errf is an implementation of L<Role::HasMessage> that uses
150             L<String::Errf> to format C<sprintf>-like message strings. It adds a
151             C<message_fmt> attribute, initialized by the C<message> argument. The value
152             should be a String::Errf format string.
153              
154             When the provided C<message> method is called, it will fill in the format
155             string with the hashref returned by calling the C<payload> method, which I<must
156             be implemented by the including class>.
157              
158             Role::HasMessage::Errf is a L<parameterized role|MooseX::Role::Parameterized>.
159             The C<default> parameter lets you set a default format string or callback. The
160             C<lazy> parameter sets whether or not the C<message_fmt> attribute is lazy.
161             Setting it lazy will require that a default is provided.
162              
163             =head1 PERL VERSION
164              
165             This library should run on perls released even a long time ago. It should work
166             on any version of perl released in the last five years.
167              
168             Although it may work on older versions of perl, no guarantee is made that the
169             minimum required version will not be increased. The version may be increased
170             for any reason, and there is no promise that patches will be accepted to lower
171             the minimum required perl.
172              
173             =head1 AUTHOR
174              
175             Ricardo Signes <cpan@semiotic.systems>
176              
177             =head1 COPYRIGHT AND LICENSE
178              
179             This software is copyright (c) 2022 by Ricardo Signes.
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =cut