浏览代码

Implemented warnings to SugarTrail::Template::Condition

Method match() now returns result *and* a reference to list of warnings
Alois Mahdal 11 年前
父节点
当前提交
3e58bfdafa
共有 1 个文件被更改,包括 26 次插入8 次删除
  1. 26
    8
      lib/SugarTrail/Template/Condition.pm

+ 26
- 8
lib/SugarTrail/Template/Condition.pm 查看文件

31
         '=~' => \&_regex,
31
         '=~' => \&_regex,
32
     };
32
     };
33
     $self->{parsed} = 0;
33
     $self->{parsed} = 0;
34
+    $self->{warnings} = [];
34
     return bless $self, $class;
35
     return bless $self, $class;
35
 }
36
 }
36
 
37
 
66
 }
67
 }
67
 
68
 
68
 # match the cond against params
69
 # match the cond against params
69
-sub match {
70
+sub __match {
70
     my $self    = shift;
71
     my $self    = shift;
71
     my $params  = shift;
72
     my $params  = shift;
72
-    $self->parse();
73
 
73
 
74
     my $name    = $self->{n};       # what is the topic
74
     my $name    = $self->{n};       # what is the topic
75
     my $op      = $self->{o};       # what op to use
75
     my $op      = $self->{o};       # what op to use
76
     my $have    = $self->{v};       # what we have in this step
76
     my $have    = $self->{v};       # what we have in this step
77
     my $want    = $params->{$name}; # what they want
77
     my $want    = $params->{$name}; # what they want
78
 
78
 
79
+    # warn if condition cannot be decided due to missing param
79
     unless ($want) {
80
     unless ($want) {
80
-        warn sprintf (
81
-            "undecided condition: %s; did you forget to specify %s?\n",
81
+        $self->warn( sprintf (
82
+            "undecided condition: %s; did you forget to specify %s?",
82
             $self->{string},
83
             $self->{string},
83
             $name
84
             $name
84
-        );
85
+        ));
85
         return 1;
86
         return 1;
86
     }
87
     }
87
 
88
 
88
-    # execute the right handler
89
+    # execute the right handler or warn it does not exist
89
     if (exists $self->{handlers}->{$op}) {
90
     if (exists $self->{handlers}->{$op}) {
90
         return &{ $self->{handlers}->{$op} }($want, $have);
91
         return &{ $self->{handlers}->{$op} }($want, $have);
91
     } else {
92
     } else {
92
-        helper::dmupp(bad_op=>$self);
93
-        warn "unknown operator: $op\n";
93
+        $self->warn("unknown operator: $op");
94
+        return 1;
94
     }
95
     }
95
 }
96
 }
96
 
97
 
98
+# wrapper to __match(); adds warning list
99
+sub match {
100
+    my $self    = shift;
101
+    my $params  = shift;
102
+    $self->parse();
103
+    $self->{result} = $self->__match($params);
104
+    return $self->{result}, $self->{warnings};
105
+}
106
+
107
+sub warn {
108
+    my $self = shift;
109
+    my $text = shift;
110
+    push @{$self->{warnings}}, {
111
+        time    => time,
112
+        text    => $text
113
+    };
114
+}
97
 
115
 
98
 1;
116
 1;