Browse Source

Moved condition parsing and evaluation to separate class (away from CondBlock)

Alois Mahdal (@azzgoat) 11 years ago
parent
commit
359d0f10b6
3 changed files with 90 additions and 20 deletions
  1. 4
    14
      lib/SugarTrail/Template/CondBlock.pm
  2. 78
    0
      lib/SugarTrail/Template/Condition.pm
  3. 8
    6
      t/Cond.t

+ 4
- 14
lib/SugarTrail/Template/CondBlock.pm View File

@@ -7,6 +7,7 @@ package SugarTrail::Template::CondBlock;
7 7
 use strict;
8 8
 use warnings;
9 9
 
10
+use SugarTrail::Template::Condition;
10 11
 use Data::Dumper;
11 12
 
12 13
 sub init {
@@ -18,23 +19,12 @@ sub init {
18 19
 sub parse {
19 20
     my $self    = shift;
20 21
     my $string  = shift;
21
-
22 22
     my @parts = split ";", $string;
23
-
24 23
     foreach (@parts) {
25
-        s/^\s+//;
26
-        s/\s+$//;
27
-        my ($n, $o, $v) = m/([a-zA-Z0-9_ ]+)([<>=~+\-*@]*)(.*)/;
28
-        next unless $n;
29
-
30
-        $n =~ s/\s+$//;
31
-        $o =~ s/\s+$//;
32
-        $o =~ s/^\s+//;
33
-        $v =~ s/\s+$//;
34
-
35
-        my @v = split m/\s*,\s*/, $v;
36
-        push @{$self->{conds}}, { n => $n, o => $o, v => \@v };
24
+        my $c = SugarTrail::Template::Condition->new($_);
25
+        push @{$self->{conds}}, $c;
37 26
     }
27
+    return scalar @{ $self->{conds} };
38 28
 }
39 29
 
40 30
 

+ 78
- 0
lib/SugarTrail/Template/Condition.pm View File

@@ -0,0 +1,78 @@
1
+package SugarTrail::Template::Condition;
2
+# condition
3
+# new()
4
+# parse(string)
5
+# match(data)
6
+
7
+use strict;
8
+use warnings;
9
+
10
+sub _equals {
11
+    my $a = shift;
12
+    my $b = $_[0]->[0];
13
+    return $a eq $b;
14
+}
15
+
16
+sub new {
17
+    my $class   = shift;
18
+    my $self    = {};
19
+    $self->{string} = shift;
20
+    $self->{handlers} = {
21
+        '==' => \&_equals,
22
+        '>=' => \&_greater_or_eq,
23
+        '<=' => \&_less_or_eq,
24
+        '=@' => \&_in,
25
+        '=~' => \&_regex,
26
+    };
27
+    $self->{parsed} = 0;
28
+    return bless $self, $class;
29
+}
30
+
31
+# parse condition from its string version
32
+sub parse {
33
+    my $self    = shift;
34
+    my $string  = $self->{string};
35
+    return if $self->{parsed};
36
+
37
+    # cut spaces
38
+    $string =~ s/^\s+//;
39
+    $string =~ s/\s+$//;
40
+
41
+    # get name, operator, value(s)
42
+    my ($n, $o, $v) = $string =~ m/([a-zA-Z0-9_ ]+)([<>=~+\-*@]*)(.*)/;
43
+    return unless $n;
44
+
45
+    # cut inner spaces
46
+    $n =~ s/\s+$//;
47
+    $o =~ s/\s+$//;
48
+    $o =~ s/^\s+//;
49
+    $v =~ s/\s+$//;
50
+
51
+    # split vlues
52
+    my @v = split m/\s*,\s*/, $v;
53
+
54
+    # assign
55
+    $self->{n} = $n;
56
+    $self->{o} = $o;
57
+    $self->{v} = \@v;
58
+
59
+    $self->{parsed}++;
60
+}
61
+
62
+# match the cond against params
63
+sub match {
64
+    my $self    = shift;
65
+    my $params  = shift;
66
+    $self->parse();
67
+
68
+    my $name    = $self->{n};       # what is the topic
69
+    my $op      = $self->{o};       # what op to use
70
+    my $have    = $self->{v};       # what we have in this step
71
+    my $want    = $params->{$name}; # what they want
72
+
73
+    # execute the right handler
74
+    return &{ $self->{handlers}->{$op} }($want, $have);
75
+}
76
+
77
+
78
+1;

+ 8
- 6
t/Cond.t View File

@@ -3,14 +3,16 @@ use Test::More;
3 3
 use lib 'lib';
4 4
 use helper;
5 5
 
6
-BEGIN { use_ok( 'SugarTrail::Template::CondBlock' ); }
7
-require_ok( 'SugarTrail::Template::CondBlock' );
6
+BEGIN { use_ok( 'SugarTrail::Template::Condition' ); }
7
+require_ok( 'SugarTrail::Template::Condition' );
8 8
 
9
-my $cb = SugarTrail::Template::CondBlock->init();
10
-ok($cb, "cb is true");
9
+my $params = { os => "wxx", proto => "imap" };
11 10
 
12
-$cb->parse('os=@w7a,wxx;proto==imap;ver>=2012;');
13
-&helper::dmupp(cb=>$cb);
11
+my $c = SugarTrail::Template::Condition->new('proto==imap');
12
+ok($c, "c is true");
13
+ok($c->match($params), "params match");
14 14
 
15 15
 
16
+&helper::dmupp(condition=>$c);
16 17
 done_testing();
18
+# os=@w7a,wxx;proto==imap;ver>=2012;