Explorar el Código

Refactored master parsing code for readability

Alois Mahdal (@azzgoat) hace 12 años
padre
commit
1e62273b1e
Se han modificado 2 ficheros con 20 adiciones y 23 borrados
  1. 15
    18
      lib/SugarTrail/Template/Master.pm
  2. 5
    5
      t/Repo.t

+ 15
- 18
lib/SugarTrail/Template/Master.pm Ver fichero

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

+ 5
- 5
t/Repo.t Ver fichero

@@ -12,12 +12,12 @@ ok($repo,           "repo is true");
12 12
 my $stm = $repo->load_stm(stm=>'/release/rc.stm');
13 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 22
 my $sts = $stm->generate_slave({ proto => "imap", os => "wxx" });
23 23
 ok($sts,            "slave is true");