blib/lib/Tail/Tool/Plugin/Spacing.pm

filter: Statementstmt 91.0% 51 / 56 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 91.0% 71 / 78 CC 7 SCAR 8.7
CC
7
Cov
91%
__ANON__
4.0
process
4.0
summarise
2.0
CRAP 2.4
« blib/lib/Tail/Tool/Plugin/Match.pm ↑ Summary blib/lib/Tail/Tool/Plugin/Ignore.pm »
1 package Tail::Tool::Plugin::Spacing;
2  
3 # Created on: 2010-10-06 14:17:00
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::MoreUtils qw/pairwise/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 extends 'Tail::Tool::PreProcess';
17  
18 3 our $VERSION = version->new('0.4.5');
19  
20 3 has last_time => (
21 is => 'rw',
22 isa => 'Int',
23 init_arg => undef,
24 );
25 has times => (
26 is => 'rw',
27 isa => 'ArrayRef[Int]',
28 2 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
29 3 );
30 has lines => (
31 is => 'rw',
32 isa => 'ArrayRef[Int]',
33 2 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
34 3 );
35 3 has '+many' => (
36 default => 0,
37 );
38  
39 around BUILDARGS => sub {
40 3 ▶ my ($orig, $class, @params) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
41 3 my %param;
42  
43 3 ▶ if ( ref $params[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 2 1 3
44 2 %param = %{ shift @params };
45 }
46 else {
47 1 %param = @params;
48 }
49  
50 3 for my $param ( keys %param) {
51 2 my $value = $param{$param};
52 2 ▶ if ( !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 0 2 2
53 0 $value = [ split /,/xms, $value ];
54 }
55 2 $param{$param} = $value;
56 }
57  
58 3 return $class->$orig(%param);
59 3 };
60  
61 sub process {
62 3 ▶ my ( $self, $line ) = @_;
Subroutine process: called CC=4 SCAR=13.9
Pod documented
63 3 my @lines = ($line);
64  
65 3 my $last = $self->last_time;
66 3 $self->last_time(time);
67 3 ▶ return @lines if !$last;
Branch: 1 branch
BranchTrueFalseTotal
unless $last 1 2 3
68  
69 2 my $diff = time - $last;
70  
71 2 ▶ for my $time ( pairwise {[$a, $b]} @{ $self->times }, @{ $self->lines } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 4 ▶ unshift @lines, ("\n") x $time->[1] if $diff >= $time->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $diff >= $time->[0] 3 1 4
73 }
74  
75 2 return @lines;
76 }
77  
78 sub summarise {
79 0 ▶ my ($self) = @_;
Subroutine summarise: not called CC=1 SCAR=6.9
Pod documented
80  
81 0 return "times = " . ( join ', ', @{ $self->times } ) . ", lines = " . ( join ', ', @{ $self->lines } );
82 }
83  
84 3 1;
85  
86 __END__
87  
88 =head1 NAME
89  
90 Tail::Tool::Plugin::Spacing - Prints spaces when there has been a pause in running.
91  
92 =head1 VERSION
93  
94 This documentation refers to Tail::Tool::Plugin::Spacing version 0.4.5.
95  
96 =head1 SYNOPSIS
97  
98 use Tail::Tool::Plugin::Spacing;
99  
100 # Brief but working code example(s) here showing the most common usage(s)
101 # This section will be as far as many users bother reading, so make it as
102 # educational and exemplary as possible.
103  
104 my $sp = Tail::Tool::Plugin::Spacing(
105 times => [ 2, 5 ],
106 lines => [ 2, 5 ],
107 );
108  
109 $sp->process("test\n");
110 # returns ("test\n");
111  
112 ...
113  
114 # 2 seconds later
115 $sp->process("test\n");
116 # returns ( "\n", "\n", "test\n" );
117  
118 ...
119  
120 # another 5 seconds later
121 $sp->process("test\n");
122 # returns ( "\n", "\n", "\n", "\n", "\n", "\n", "\n", "test\n" );
123 # ie 7 blank lines ( 2 lines + 5 lines )
124  
125 =head1 DESCRIPTION
126  
127 =head1 SUBROUTINES/METHODS
128  
129 =head2 C<new (%params)>
130  
131 Param: C<times> - [int] - The minimum time (in seconds) for a pause to be
132 considered to have occurred, resulting in the corresponding number of lines
133 (in the C<lines> argument) prepended to the found line.
134  
135 Param: C<lines> - [int] - The number of lines to print when the corresponding
136 period in C<times> is reached.
137  
138 Description: create a new object
139  
140 =head2 C<process ()>
141  
142 Description: Prints spaces based on time between last call and this one and
143 the settings.
144  
145 =head2 C<summarise ()>
146  
147 Returns a string that summarise the current settings of the plugin instance
148  
149 =head1 DIAGNOSTICS
150  
151 =head1 CONFIGURATION AND ENVIRONMENT
152  
153 =head1 DEPENDENCIES
154  
155 =head1 INCOMPATIBILITIES
156  
157 =head1 BUGS AND LIMITATIONS
158  
159 There are no known bugs in this module.
160  
161 Please report problems to Ivan Wills (ivan.wills@gamil.com).
162  
163 Patches are welcome.
164  
165 =head1 AUTHOR
166  
167 Ivan Wills - (ivan.wills@gamil.com)
168 <Author name(s)> (<contact address>)
169  
170 =head1 LICENSE AND COPYRIGHT
171  
172 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia, 2077).
173 All rights reserved.
174  
175 This module is free software; you can redistribute it and/or modify it under
176 the same terms as Perl itself. See L<perlartistic>. This program is
177 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
178 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
179 PARTICULAR PURPOSE.
180  
181 =cut
« blib/lib/Tail/Tool/Plugin/Match.pm ↑ Summary blib/lib/Tail/Tool/Plugin/Ignore.pm »