File Coverage

blib/lib/Markdent/Role/BalancedEvent.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition 3 9 33.3
subroutine 17 17 100.0
pod n/a
total 55 63 87.3


line stmt bran cond sub pod time code
1             package Markdent::Role::BalancedEvent;
2              
3 35     47   28509 use strict;
  35         89  
  35         1277  
4 35     47   198 use warnings;
  35         77  
  35         981  
5 35     35   188 use namespace::autoclean;
  35         177  
  35         263  
6              
7             our $VERSION = '0.39';
8              
9 35     35   3179 use List::AllUtils qw( all );
  35         77  
  35         2354  
10 35     35   249 use Markdent::Types;
  35         75  
  35         303  
11 35     35   852658 use Params::ValidationCompiler qw( validation_for );
  35         92  
  35         2537  
12 35     35   256 use Specio::Declare;
  35         126  
  35         412  
13              
14 35     35   7576 use MooseX::Role::Parameterized;
  35         83  
  35         474  
15              
16             parameter compare => (
17             isa => t( 'ArrayRef', of => t('Str') ),
18             );
19              
20             role {
21             my $p = shift;
22              
23             my @compare = @{ $p->compare || [] };
24              
25             my $validator = validation_for(
26             params => [
27             { type => t('EventObject') },
28             ],
29             );
30              
31             method balances_event => sub {
32 537     537   922 my $self = shift;
        537      
        537      
        537      
        537      
        537      
        537      
        537      
33 537         11286 my ($other) = $validator->(@_);
34              
35 537 50       38767 return 0 unless $self->name eq $other->name;
36              
37 537 50 33     1378 return 0
      33        
      33        
38             unless ( $self->is_start && $other->is_end )
39             || ( $self->is_end && $other->is_start );
40              
41 537 100       1701 return 1 unless @compare;
42              
43 473     473   3275 return all { $self->$_() eq $other->$_() } @compare;
  473         13794  
44             };
45             };
46              
47             1;
48              
49             # ABSTRACT: A parameterized role for events which can check if they balance another event
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Markdent::Role::BalancedEvent - A parameterized role for events which can check if they balance another event
60              
61             =head1 VERSION
62              
63             version 0.39
64              
65             =head1 DESCRIPTION
66              
67             This role provides behavior for start and end events which can be checked for
68             a balancing event. This includes things like strong, emphasis, and code
69             start/end events.
70              
71             =head1 ROLE PARAMETERS
72              
73             This role accepts the following parameters:
74              
75             =over 4
76              
77             =item * compare => [ ... ]
78              
79             This should be a list of attribute names which will be compared between the
80             start and end events.
81              
82             =back
83              
84             =head1 METHODS
85              
86             This role provides the following methods:
87              
88             =head2 $event->balances_event($event2)
89              
90             Given an event, this returns true if two events balance each other. This is
91             done by comparing types (StartCode matches EndCode), as well as the attributes
92             provided in the compare parameter.
93              
94             =head1 BUGS
95              
96             See L<Markdent> for bug reporting details.
97              
98             Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
99              
100             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
101              
102             =head1 SOURCE
103              
104             The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
105              
106             =head1 AUTHOR
107              
108             Dave Rolsky <autarch@urth.org>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is copyright (c) 2021 by Dave Rolsky.
113              
114             This is free software; you can redistribute it and/or modify it under
115             the same terms as the Perl 5 programming language system itself.
116              
117             The full text of the license can be found in the
118             F<LICENSE> file included with this distribution.
119              
120             =cut