[libcatmandu-perl] 37/46: Simplifying the greater_than less_than conditional

Jonas Smedegaard dr at jones.dk
Tue Oct 14 13:52:54 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9205
in repository libcatmandu-perl.

commit 449072dc4e8f617febd1e9dfeea2677b6a11ad51
Author: Patrick Hochstenbach <patrick.hochstenbach at ugent.be>
Date:   Thu Oct 2 16:39:16 2014 +0200

    Simplifying the greater_than less_than conditional
---
 lib/Catmandu/Fix/Condition/greater_than.pm | 55 ++++++++++++------------------
 lib/Catmandu/Fix/Condition/less_than.pm    | 53 ++++++++++++----------------
 2 files changed, 43 insertions(+), 65 deletions(-)

diff --git a/lib/Catmandu/Fix/Condition/greater_than.pm b/lib/Catmandu/Fix/Condition/greater_than.pm
index e4d233b..df973ec 100644
--- a/lib/Catmandu/Fix/Condition/greater_than.pm
+++ b/lib/Catmandu/Fix/Condition/greater_than.pm
@@ -1,35 +1,4 @@
 package Catmandu::Fix::Condition::greater_than;
-use Catmandu::Sane;
-use Moo;
-use Catmandu::Fix::Has;
-
-with 'Catmandu::Fix::Condition';
-
-has path    => (fix_arg => 1);
-has value   => (fix_arg => 1);
-
-sub emit {
-    my ($self, $fixer, $label) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-    my $str_key = $fixer->emit_string($key);
-    my $value = $fixer->emit_string($self->value);
-
-    my $perl = $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var  = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "if (is_value(${var}) && ${var} > $value) {" .
-            $fixer->emit_fixes($self->pass_fixes) .
-            "last $label;" .
-            "}";
-        });
-    });
-
-    $perl .= $fixer->emit_fixes($self->fail_fixes);
-
-    $perl;
-}
 
 =head1 NAME
 
@@ -37,9 +6,14 @@ Catmandu::Fix::Condition::greater_than - Excute fixes when a field is greater th
 
 =head1 SYNOPSIS
 
-   # greather_than(X,Y) is true when X > Y
+   # greater_than(X,Y) is true when X > Y
    if greater_than('year','2018')
-   	add_field('my.funny.title','true')
+    add_field('my.funny.title','true')
+   end
+
+   # greater_than on arrays checks if all values are X > Y
+   if greater_than('years.*','2018')
+     add_field('my.funny.title','true')
    end
 
 =head1 SEE ALSO
@@ -48,4 +22,19 @@ L<Catmandu::Fix::Condition::less_than>
 
 =cut
 
+use Catmandu::Sane;
+use Moo;
+use Catmandu::Fix::Has;
+
+has path    => (fix_arg => 1);
+has value   => (fix_arg => 1);
+
+with 'Catmandu::Fix::Condition::SimpleAllTest';
+
+sub emit_test {
+    my ($self, $var) = @_;
+    my $value = $self->value;
+    "is_value(${var}) && ${var} > $value";
+}
+
 1;
\ No newline at end of file
diff --git a/lib/Catmandu/Fix/Condition/less_than.pm b/lib/Catmandu/Fix/Condition/less_than.pm
index 0762cb9..aa1c7d9 100644
--- a/lib/Catmandu/Fix/Condition/less_than.pm
+++ b/lib/Catmandu/Fix/Condition/less_than.pm
@@ -1,35 +1,4 @@
 package Catmandu::Fix::Condition::less_than;
-use Catmandu::Sane;
-use Moo;
-use Catmandu::Fix::Has;
-
-with 'Catmandu::Fix::Condition';
-
-has path    => (fix_arg => 1);
-has value   => (fix_arg => 1);
-
-sub emit {
-    my ($self, $fixer, $label) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-    my $str_key = $fixer->emit_string($key);
-    my $value = $fixer->emit_string($self->value);
-
-    my $perl = $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var  = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "if (is_value(${var}) && ${var} < $value) {" .
-            $fixer->emit_fixes($self->pass_fixes) .
-            "last $label;" .
-            "}";
-        });
-    });
-
-    $perl .= $fixer->emit_fixes($self->fail_fixes);
-
-    $perl;
-}
 
 =head1 NAME
 
@@ -39,7 +8,12 @@ Catmandu::Fix::Condition::less_than - Excute fixes when a field is less than a v
 
    # less_than(X,Y) is true when X < Y
    if less_than('year','2018')
-   	add_field('my.funny.title','true')
+    add_field('my.funny.title','true')
+   end
+
+   # less_than on arrays checks if all values are X < Y
+   if less_than('years.*','2018')
+     add_field('my.funny.title','true')
    end
 
 =head1 SEE ALSO
@@ -48,4 +22,19 @@ L<Catmandu::Fix::Condition::greater_than>
 
 =cut
 
+use Catmandu::Sane;
+use Moo;
+use Catmandu::Fix::Has;
+
+has path    => (fix_arg => 1);
+has value   => (fix_arg => 1);
+
+with 'Catmandu::Fix::Condition::SimpleAllTest';
+
+sub emit_test {
+    my ($self, $var) = @_;
+    my $value = $self->value;
+    "is_value(${var}) && ${var} < $value";
+}
+
 1;
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-perl.git



More information about the Pkg-perl-cvs-commits mailing list