File Coverage

blib/lib/PDK/Content/Reader.pm
Criterion Covered Total %
statement 88 107 82.2
branch 8 16 50.0
condition 7 17 41.1
subroutine 20 21 95.2
pod 9 9 100.0
total 132 170 77.6


line stmt bran cond sub pod time code
1             package PDK::Content::Reader;
2              
3 2     2   104548 use utf8;
  2         5  
  2         16  
4 2     2   92 use v5.30;
  2         8  
5 2     2   470 use Moose;
  2         484679  
  2         15  
6 2     2   14331 use Digest::MD5;
  2         5  
  2         118  
7 2     2   2328 use DateTime;
  2         1344648  
  2         180  
8 2     2   26 use Carp qw'croak';
  2         5  
  2         222  
9 2     2   17 use namespace::autoclean;
  2         4  
  2         26  
10 2     2   1577 use Data::Dumper;
  2         23929  
  2         3895  
11              
12             has config => (is => 'ro', isa => 'ArrayRef[Str]', required => 1, );
13              
14             has confContent => (is => 'ro', isa => 'Str', lazy => 1, builder => '_buildConfContent', );
15              
16             has cursor => (is => 'ro', isa => 'Int', default => 0, );
17              
18             with 'PDK::Content::Role';
19             with 'PDK::Content::Dumper';
20              
21             has '+sign' => (required => 0, lazy => 1, builder => '_buildSign', );
22              
23             has '+timestamp' => (required => 0, builder => '_buildTimestamp', );
24              
25             sub _buildSign {
26 0     0   0 my $self = shift;
27              
28 0         0 return Digest::MD5::md5_hex(join("\n", @{$self->config}));
  0         0  
29             }
30              
31             sub _buildConfContent {
32 2     2   9 my $self = shift;
33              
34 2         9 return join("\n", @{$self->config});
  2         102  
35             }
36              
37             sub _buildTimestamp {
38 20     20   47 shift;
39              
40 20         396 return DateTime->now->strftime('%Y-%m-%d %H:%M:%S');
41             }
42              
43             sub _buildLineParsedFlags {
44 20     20   107 my $self = shift;
45              
46 20         62 return [map {0} (1 .. @{$self->config})];
  80         1248  
  20         1170  
47             }
48              
49             sub goToHead {
50 6     6 1 287 my $self = shift;
51              
52 6         47 $self->{cursor} = 0;
53 6         103 $self->dump("[goToHead] 游标已移至首行:$self->{config}->[$self->{cursor}]");
54             }
55              
56             sub nextLine {
57 34     34 1 4193 my $self = shift;
58 34         115 my $result;
59              
60 34 50       1669 if ($self->cursor < scalar(@{$self->config})) {
  34         2068  
61              
62 34         1261 $result = $self->config->[$self->cursor];
63 34         479 $self->dump("[nextLine] 获取当前游标指向的下一行内容:$result");
64              
65 34         329 $self->{cursor}++;
66             }
67              
68 34         790 return $result;
69             }
70              
71             sub prevLine {
72 2     2 1 103 my $self = shift;
73              
74 2 50       42 if ($self->{cursor} > 0) {
75 2         56 $self->dump("[prevLine] 获取当前游标指向的上一行:$self->{config}->[$self->{cursor}]");
76 2         39 $self->{cursor}--;
77 2         50 return 1;
78             }
79             else {
80 0         0 $self->dump("[prevLine] 获取当前游标指向的上一行异常:当前游标已经在第一行 $self->{cursor}");
81 0         0 warn("游标已经在头部:$self->{cursor}");
82 0         0 return undef;
83             }
84             }
85              
86             sub nextUnParsedLine {
87 28     28 1 6233 my $self = shift;
88 28         111 my $result;
89              
90 28   66     1892 while ($self->cursor < scalar(@{$self->config}) && $self->getParseFlag == 1) {
  34         1515  
91 6         252 $self->{cursor}++;
92             }
93              
94 28 50       1077 if ($self->cursor < scalar(@{$self->config})) {
  28         933  
95 28         968 $result = $self->config->[$self->cursor];
96              
97 28   33     600 while (!defined $result || $result =~ /^\s*$/m) {
98 0         0 $self->setParseFlag(1);
99 0         0 $self->{cursor}++;
100              
101 0   0     0 while ($self->cursor < scalar(@{$self->config}) && $self->getParseFlag == 1) {
  0         0  
102 0         0 $self->{cursor}++;
103             }
104              
105 0 0       0 if ($self->cursor < scalar(@{$self->config})) {
  0         0  
106 0         0 $result = $self->config->[$self->cursor];
107             }
108             else {
109 0         0 return undef;
110             }
111             }
112              
113 28         240 $self->setParseFlag(1);
114 28         95 $self->{cursor}++;
115             }
116              
117 28         78 chomp $result;
118              
119              
120 28         428 $self->dump("[nextUnParsedLine] 获取到下一个未解析的行:$result");
121 28         784 return $result;
122             }
123              
124             sub moveBack {
125 6     6 1 186 my $self = shift;
126              
127 6 50       205 if ($self->{cursor} > 0) {
128 6         57 $self->{cursor}--;
129 6         206 $self->setParseFlag(0);
130 6         252 $self->dump("[moveBack] 如果游标不在头部,则将游标向前移动并重置解析标志:" . $self->config->[$self->{cursor}]);
131 6         308 return 1;
132             }
133             else {
134 0         0 $self->dump("[moveBack] 将游标向前移动一位异常:当前游标已在行首 $self->{cursor}");
135 0         0 warn("游标已经在头部:$self->{cursor}");
136 0         0 return undef;
137             }
138             }
139              
140             sub ignore {
141 4     4 1 162 my $self = shift;
142 4         245 $self->dump("[ignore] 忽略当前行解析状态:" . $self->config->[$self->{cursor}]);
143              
144 4 50       145 return $self->moveBack ? $self->nextLine : undef;
145             }
146              
147             sub getUnParsedLines {
148 2     2 1 90 my $self = shift;
149              
150             my $unParsedLines = join('',
151 2         41 map { $self->config->[$_] } grep { $self->{lineParsedFlags}->[$_] == 0 } (0 .. scalar(@{$self->config}) - 1) );
  4         122  
  8         45  
  2         107  
152              
153 2         39 $self->dump("[getUnParsedLines] 获取所有未解析的行并打印:" . Dumper $unParsedLines);
154 2         130 return $unParsedLines;
155             }
156              
157             sub getParseFlag {
158 40     40 1 6220 my $self = shift;
159              
160 40 100 66     1361 if ($self->cursor >= 0 && $self->cursor < scalar(@{$self->config})) {
  40         1341  
161 38         1647 return $self->{lineParsedFlags}->[$self->cursor];
162             }
163              
164 2         39 return;
165             }
166              
167             sub setParseFlag {
168 36     36 1 194 my ($self, $flag) = @_;
169              
170 36 50 33     1326 if ($self->cursor >= 0 && $self->cursor < scalar(@{$self->config})) {
  36         1493  
171 36   50     1281 $self->{lineParsedFlags}->[$self->cursor] = $flag // 1;
172              
173 36         131 return 1;
174             }
175              
176 0           return;
177             }
178              
179             __PACKAGE__->meta->make_immutable;
180             1;