Browse Source

Refactored master parsing code for readability

Alois Mahdal (@azzgoat) 12 years ago
parent
commit
1e62273b1e
2 changed files with 20 additions and 23 deletions
  1. 15
    18
      lib/SugarTrail/Template/Master.pm
  2. 5
    5
      t/Repo.t

+ 15
- 18
lib/SugarTrail/Template/Master.pm View File

1
 package SugarTrail::Template::Master;
1
 package SugarTrail::Template::Master;
2
 # Master template holder
2
 # Master template holder
3
 # new()
3
 # new()
4
-# parse_headers()
5
-
6
-
4
+# parse()
5
+# _parse_headers()
6
+# _parse_body()
7
 
7
 
8
 use strict;
8
 use strict;
9
 use warnings;
9
 use warnings;
18
     return bless $self, $class;
18
     return bless $self, $class;
19
 }
19
 }
20
 
20
 
21
-sub parse_headers {
21
+sub parse {
22
     my $self = shift;
22
     my $self = shift;
23
-
24
     my ($head, $body) = split "\n\n", $self->{text}, 2;
23
     my ($head, $body) = split "\n\n", $self->{text}, 2;
25
-
26
-    my @lines = split "\n", $head;
27
-
28
     $self->{head} = $head;
24
     $self->{head} = $head;
29
     $self->{body} = $body;
25
     $self->{body} = $body;
26
+    return (
27
+        $self->_parse_headers(),
28
+        $self->_parse_body()
29
+    );
30
+}
30
 
31
 
31
-    foreach (@lines) {
32
+sub _parse_headers {
33
+    my $self    = shift;
34
+    foreach (split "\n", $self->{head}) {
32
         my ($n, $v) = split ": ", $_, 2;
35
         my ($n, $v) = split ": ", $_, 2;
33
         $self->{meta}->{$n} = $v;
36
         $self->{meta}->{$n} = $v;
34
     }
37
     }
35
-
36
     return scalar keys %{ $self->{meta} };
38
     return scalar keys %{ $self->{meta} };
37
 }
39
 }
38
 
40
 
39
-sub parse_body {
41
+sub _parse_body {
40
     my $self = shift;
42
     my $self = shift;
41
-    my @lines = split "\n", $self->{body};
42
-
43
     $self->{steps} = [];
43
     $self->{steps} = [];
44
-
45
-    foreach (@lines) {
46
-        chomp;
47
-        s/ *$//;
44
+    foreach (split "\n", $self->{body}) {
45
+        chomp; s/ *$//;
48
         my ($line, $cond) = $_ =~ m/^(.*?)(\{(.*)\})?$/;
46
         my ($line, $cond) = $_ =~ m/^(.*?)(\{(.*)\})?$/;
49
         $line =~ s/ *$//;
47
         $line =~ s/ *$//;
50
         push @{ $self->{steps} }, { line => $line, cond => $cond };
48
         push @{ $self->{steps} }, { line => $line, cond => $cond };
51
     }
49
     }
52
-
53
     return scalar @{ $self->{steps} };
50
     return scalar @{ $self->{steps} };
54
 }
51
 }
55
 
52
 

+ 5
- 5
t/Repo.t View File

12
 my $stm = $repo->load_stm(stm=>'/release/rc.stm');
12
 my $stm = $repo->load_stm(stm=>'/release/rc.stm');
13
 ok($stm,            "master is true");
13
 ok($stm,            "master is true");
14
 
14
 
15
-ok($stm->parse_headers(), "parse_headers() returns 6");
16
-ok($stm->parse_headers(), "parse_headers() still returns 6");
17
-is($stm->parse_body(), 13, "parse_body() returns 13");
18
-is($stm->parse_body(), 13, "parse_body() still returns 13");
15
+my ($hc,$bc) = $stm->parse();
19
 
16
 
20
-&helper::dmupp(master=>$stm);
17
+is($hc, 6,  "parse() reported 6 headers");
18
+is($bc, 13, "parse() reported 13 non-header lines");
19
+
20
+&helper::dmup(master=>$stm);
21
 
21
 
22
 my $sts = $stm->generate_slave({ proto => "imap", os => "wxx" });
22
 my $sts = $stm->generate_slave({ proto => "imap", os => "wxx" });
23
 ok($sts,            "slave is true");
23
 ok($sts,            "slave is true");