File Coverage

blib/lib/Perl/Tidy/Sweetened/Pluggable.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition 3 4 75.0
subroutine 9 9 100.0
pod 0 7 0.0
total 40 48 83.3


line stmt bran cond sub pod time code
1             package Perl::Tidy::Sweetened::Pluggable;
2              
3             # ABSTRACT: Simple object to facilitate a pluggable filter architecture
4              
5 13     13   109 use strict;
  13         36  
  13         441  
6 13     13   78 use warnings;
  13         51  
  13         4490  
7              
8             our $VERSION = '1.18';
9              
10             sub new {
11 13     13 0 180 my ( $class, %args ) = @_;
12 13         88 return bless {%args}, $class;
13             }
14              
15 331   100 331 0 1969 sub filters { return ( $_[0]->{filters} ||= [] ) }
16              
17 2068   50 2068 0 8880 sub args { return ( $_[0]->{args} ||= {} ) }
18              
19             sub add_filter {
20 143     143 0 238 my $self = shift;
21 143         203 push @{ $self->filters }, @_;
  143         251  
22             }
23              
24             sub prefilter {
25 94     94 0 434 my ( $self, $code ) = @_;
26 94         221 for my $filter ( @{ $self->filters } ) {
  94         409  
27 1034         2551 $code = $filter->prefilter($code, $self->args);
28             }
29              
30             # warn "After prefilter, before tidy\n";
31             # warn $code;
32              
33 94         371 return $code;
34             }
35              
36             sub postfilter {
37 94     94 0 348 my ( $self, $code ) = @_;
38              
39             # warn "After tidy, before postfilter\n";
40             # warn $code;
41              
42 94         215 for my $filter ( @{ $self->filters } ) {
  94         445  
43 1034         2724 $code = $filter->postfilter($code, $self->args);
44             }
45 94         386 return $code;
46             }
47              
48             sub add_args {
49 94     94 0 307 my ( $self, $args ) = @_;
50 94         769 my ( $body_parts, $msg ) = Perl::Tidy::parse_args($args);
51             $self->{args} =
52 94         13338 { map { my ( $k, $v ) = split /=/, $_, 2; ( $k => $v ) } @$body_parts };
  385         1117  
  385         1531  
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =head1 NAME
62              
63             Perl::Tidy::Sweetened::Pluggable - Simple object to facilitate a pluggable filter architecture
64              
65             =head1 VERSION
66              
67             version 1.18
68              
69             =head1 SYNOPSIS
70              
71             our $plugins = Perl::Tidy::Sweetened::Pluggable->new();
72              
73             $plugins->add_filter(
74             Perl::Tidy::Sweetened::Keyword::SubSignature->new(
75             keyword => 'method',
76             marker => 'METHOD',
77             ) );
78              
79             $plugins->prefilter( $code );
80             $plugins->postfilter( $code );
81              
82             =head1 DESCRIPTION
83              
84             Builds a pluggable, chainable list of filters.
85              
86             =head1 AUTHOR
87              
88             Mark Grimes E<lt>mgrimes@cpan.orgE<gt>
89              
90             =head1 SOURCE
91              
92             Source repository is at L<https://github.com/mvgrimes/Perl-Tidy-Sweetened>.
93              
94             =head1 BUGS
95              
96             Please report any bugs or feature requests on the bugtracker website L<http://github.com/mvgrimes/Perl-Tidy-Sweetened/issues>
97              
98             When submitting a bug or request, please include a test-file or a
99             patch to an existing test-file that illustrates the bug or desired
100             feature.
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             This software is copyright (c) 2021 by Mark Grimes E<lt>mgrimes@cpan.orgE<gt>.
105              
106             This is free software; you can redistribute it and/or modify it under
107             the same terms as the Perl 5 programming language system itself.
108              
109             =cut