File Coverage

blib/lib/Markdent/Role/Event.pm
Criterion Covered Total %
statement 36 66 54.5
branch 2 10 20.0
condition 3 12 25.0
subroutine 205 230 89.1
pod 1 1 100.0
total 247 319 77.4


line stmt bran cond sub pod time code
1             package Markdent::Role::Event;
2              
3 35     6596   24499 use strict;
  35         91  
  35         1187  
4 35     6596   231 use warnings;
  35         82  
  35         1054  
5 35     6596   190 use namespace::autoclean;
  35         75  
  35         244  
6              
7             our $VERSION = '0.39';
8              
9 35     3092   2850 use Markdent::Types;
  35         86  
  35         332  
10              
11 35     3092   858317 use MooseX::Role::Parameterized;
  35         2116576  
  35         157  
12              
13             parameter event_class => (
14             is => 'ro',
15             isa => t('Str'),
16             required => 1,
17             );
18              
19             role {
20             my $p = shift;
21              
22             my $class = $p->event_class;
23             my ( $action, $event ) = $class =~ /::(Start|End)?(\w+)$/;
24              
25             # It's easier to hack this in rather than trying to find a general
26             # case for upper-case abbreviations in class names.
27             $event =~ s/HTML/html/;
28              
29             $event =~ s/(^|.)([A-Z])/$1 ? "$1\L_$2" : "\L$2"/ge;
30              
31             my $event_name = join q{_}, map {lc} grep {defined} $action, $event;
32 15033     18090   29183 method event_name => sub {$event_name};
        18090      
        15071      
        15071      
        15071      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        15033      
        2568      
        2410      
        2410      
33              
34 1074     1074   3634 method name => sub {$event};
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        1074      
        2410      
        2410      
        2410      
35              
36             my $is_start = ( $action || q{} ) eq 'Start';
37 2173     2197   8307 method is_start => sub {$is_start};
        2197      
        2197      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2173      
        2410      
        2410      
        32      
38              
39             my $is_end = ( $action || q{} ) eq 'End';
40 1492     1542   5429 method is_end => sub {$is_end};
        1542      
        1542      
        1494      
        1494      
        1494      
        1494      
        1494      
        1494      
        1494      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        1492      
        32      
        32      
41              
42             my $is_inline = !defined $action;
43 0     43   0 method is_inline => sub {$is_inline};
        43      
        43      
        2      
        2      
        2      
        2      
        2      
        2      
        2      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
44              
45             my @required;
46             my @optional;
47              
48             for my $attr ( grep { $_->name !~ /^_/ }
49             $class->meta->get_all_attributes ) {
50              
51             my $name = $attr->name;
52              
53             if ( $attr->is_required ) {
54             push @required, [ $name, $attr->get_read_method ];
55             }
56             else {
57             die
58             "All optional attributes for an event must have a predicate or default value ($class - $name)"
59             unless $attr->has_predicate
60             || $attr->has_default
61             || $attr->has_builder;
62              
63             push @optional,
64             [
65             $name,
66             $attr->get_read_method,
67             $attr->predicate
68             ];
69             }
70             }
71              
72             _make_kv_pairs_for_attribute_method( \@required, \@optional );
73             };
74              
75             sub _make_kv_pairs_for_attribute_method {
76 1280     1280   2771 my @required = @{ shift() };
  1280         4060  
77 1280         2596 my @optional = @{ shift() };
  1280         2983  
78              
79             method kv_pairs_for_attributes => sub {
80 5469     5469   8103 my $event = shift;
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
        5469      
81              
82 5469         7573 my %p;
83              
84 5469         10649 for my $pair (@required) {
85 3656         4848 my ( $name, $reader ) = @{$pair};
  3656         7638  
86              
87 3656         93563 $p{$name} = $event->$reader();
88             }
89              
90 5469         9358 for my $triplet (@optional) {
91 1423         1743 my ( $name, $reader, $pred ) = @{$triplet};
  1423         3019  
92              
93 1423 100 100     6046 next if $pred && !$event->$pred();
94              
95 1367         35536 $p{$name} = $event->$name();
96             }
97              
98 5469         26806 return %p;
99 1280         10980 };
100             }
101              
102             sub debug_dump {
103 0     2568 1   my $self = shift;
104              
105 0           my $dump = ' - ' . $self->event_name . "\n";
106              
107 0           for my $attr ( sort { $a->name cmp $b->name }
  0            
108             $self->meta->get_all_attributes ) {
109 0           my $name = $attr->name;
110 0           my $reader = $attr->get_read_method;
111 0           my $pred = $attr->predicate;
112              
113 0 0 0       next if $pred && !$self->$pred();
114              
115 0           my $val = $self->$reader();
116              
117 0 0 0       if ( ref $val && ref $val eq 'ARRAY' ) {
    0 0        
118 0           $dump .= sprintf( ' %-16s: |%s|', $name, $val->[0] );
119 0           $dump .= "\n";
120              
121 0           for my $v ( @{$val}[ 1 .. $#{$val} ] ) {
  0            
  0            
122 0           $self->_debug_value($v);
123              
124 0           $dump .= q{ } x 22;
125 0           $dump .= "|$v|\n";
126             }
127             }
128             elsif ( ref $val && ref $val eq 'HASH' ) {
129 0           $dump .= sprintf( ' %-16s:', $name );
130 0           $dump .= "\n";
131              
132 0           for my $k ( sort keys %{$val} ) {
  0            
133 0           $dump .= q{ } x 22;
134             $dump .= sprintf(
135             '%-16s: %s', $k,
136 0           $self->_debug_value( $val->{$k} )
137             );
138 0           $dump .= "\n";
139             }
140             }
141             else {
142 0           $dump .= sprintf(
143             ' %-16s: %s', $name,
144             $self->_debug_value($val)
145             );
146 0           $dump .= "\n";
147             }
148             }
149              
150 0           return $dump;
151             }
152              
153             sub _debug_value {
154 0 0   2568     return defined $_[1] ? $_[1] : '<undef>';
155             }
156              
157             1;
158              
159             # ABSTRACT: Implements behavior shared by all events
160              
161             __END__
162              
163             =pod
164              
165             =encoding UTF-8
166              
167             =head1 NAME
168              
169             Markdent::Role::Event - Implements behavior shared by all events
170              
171             =head1 VERSION
172              
173             version 0.39
174              
175             =head1 DESCRIPTION
176              
177             This role provides shared behavior for all event classes. It is actually
178             somewhat of a hack, as it is a parameterized role that generates methods for
179             each class that consumes it.
180              
181             =head1 METHODS
182              
183             This role provides the following methods:
184              
185             =head2 $event->is_start
186              
187             =head2 $event->is_end
188              
189             =head2 $event->is_inline
190              
191             These all returns booleans indicating whether the event is of the specified
192             type.
193              
194             =head2 $event->event_name
195              
196             This returns a name like "start_blockquote", "end_strong", or "text".
197              
198             =head2 $event->kv_pairs_for_attributes
199              
200             This returns a hash representing the data stored in the object's
201             attributes. If an attribute is not required and has not been set, it will not
202             be present in the hash.
203              
204             =head2 $event->debug_dump
205              
206             Returns a string representation of the event suitable for debugging output.
207              
208             =head1 BUGS
209              
210             See L<Markdent> for bug reporting details.
211              
212             Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
213              
214             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
215              
216             =head1 SOURCE
217              
218             The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
219              
220             =head1 AUTHOR
221              
222             Dave Rolsky <autarch@urth.org>
223              
224             =head1 COPYRIGHT AND LICENSE
225              
226             This software is copyright (c) 2021 by Dave Rolsky.
227              
228             This is free software; you can redistribute it and/or modify it under
229             the same terms as the Perl 5 programming language system itself.
230              
231             The full text of the license can be found in the
232             F<LICENSE> file included with this distribution.
233              
234             =cut