Ver código fonte

Implemented warnings to SugarTrail::Template::Condition

Method match() now returns result *and* a reference to list of warnings
Alois Mahdal 12 anos atrás
pai
commit
3e58bfdafa
1 arquivos alterados com 26 adições e 8 exclusões
  1. 26
    8
      lib/SugarTrail/Template/Condition.pm

+ 26
- 8
lib/SugarTrail/Template/Condition.pm Ver arquivo

@@ -31,6 +31,7 @@ sub new {
31 31
         '=~' => \&_regex,
32 32
     };
33 33
     $self->{parsed} = 0;
34
+    $self->{warnings} = [];
34 35
     return bless $self, $class;
35 36
 }
36 37
 
@@ -66,33 +67,50 @@ sub parse {
66 67
 }
67 68
 
68 69
 # match the cond against params
69
-sub match {
70
+sub __match {
70 71
     my $self    = shift;
71 72
     my $params  = shift;
72
-    $self->parse();
73 73
 
74 74
     my $name    = $self->{n};       # what is the topic
75 75
     my $op      = $self->{o};       # what op to use
76 76
     my $have    = $self->{v};       # what we have in this step
77 77
     my $want    = $params->{$name}; # what they want
78 78
 
79
+    # warn if condition cannot be decided due to missing param
79 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 83
             $self->{string},
83 84
             $name
84
-        );
85
+        ));
85 86
         return 1;
86 87
     }
87 88
 
88
-    # execute the right handler
89
+    # execute the right handler or warn it does not exist
89 90
     if (exists $self->{handlers}->{$op}) {
90 91
         return &{ $self->{handlers}->{$op} }($want, $have);
91 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 116
 1;