[libcatmandu-perl] 11/16: diy simple fix boillerplate

Jonas Smedegaard dr at jones.dk
Thu Dec 4 14:43:17 UTC 2014


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

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

commit ce22590299d810ff458255c6edc2c8f54e7b68c4
Author: Nicolas Steenlant <nicolas.steenlant at ugent.be>
Date:   Tue Dec 2 20:22:06 2014 +0100

    diy simple fix boillerplate
---
 .../{SimpleSetValue.pm => SimpleChangeValue.pm}    |  6 ++--
 lib/Catmandu/Fix/append.pm                         |  8 ++---
 lib/Catmandu/Fix/array.pm                          | 23 ++++---------
 lib/Catmandu/Fix/capitalize.pm                     | 19 +++--------
 lib/Catmandu/Fix/count.pm                          | 27 +++++----------
 lib/Catmandu/Fix/downcase.pm                       | 19 +++--------
 lib/Catmandu/Fix/from_json.pm                      | 31 ++++++++----------
 lib/Catmandu/Fix/hash.pm                           | 23 ++++---------
 lib/Catmandu/Fix/join_field.pm                     | 22 ++++---------
 lib/Catmandu/Fix/prepend.pm                        | 19 +++--------
 lib/Catmandu/Fix/replace_all.pm                    | 24 +++++---------
 lib/Catmandu/Fix/trim.pm                           | 38 +++++++++-------------
 lib/Catmandu/Fix/upcase.pm                         | 19 +++--------
 13 files changed, 91 insertions(+), 187 deletions(-)

diff --git a/lib/Catmandu/Fix/SimpleSetValue.pm b/lib/Catmandu/Fix/SimpleChangeValue.pm
similarity index 75%
rename from lib/Catmandu/Fix/SimpleSetValue.pm
rename to lib/Catmandu/Fix/SimpleChangeValue.pm
index 6155227..e53eaff 100644
--- a/lib/Catmandu/Fix/SimpleSetValue.pm
+++ b/lib/Catmandu/Fix/SimpleChangeValue.pm
@@ -1,4 +1,4 @@
-package Catmandu::Fix::SimpleSetValue;
+package Catmandu::Fix::SimpleChangeValue;
 
 use Catmandu::Sane;
 use Moo::Role;
@@ -6,7 +6,7 @@ use Moo::Role;
 with 'Catmandu::Fix::Base';
 
 requires 'path';
-requires 'emit_value';
+requires 'emit_change_value';
 
 sub emit {
     my ($self, $fixer) = @_;
@@ -17,7 +17,7 @@ sub emit {
         my $var = shift;
         $fixer->emit_get_key($var, $key, sub {
             my $var = shift;
-            "${var} = " . $self->emit_value($var, $fixer);
+            $self->emit_change_value($var, $fixer);
         });
     });
 }
diff --git a/lib/Catmandu/Fix/append.pm b/lib/Catmandu/Fix/append.pm
index f1de9f6..8273bda 100644
--- a/lib/Catmandu/Fix/append.pm
+++ b/lib/Catmandu/Fix/append.pm
@@ -4,15 +4,15 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::SimpleSetValue';
-
 has path  => (fix_arg => 1);
 has value => (fix_arg => 1);
 
-sub emit_value {
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
     my ($self, $var, $fixer) = @_;
     my $value = $fixer->emit_string($self->value);
-    "join('', ${var}, $value) if is_value(${var});";
+    "${var} = join('', ${var}, $value) if is_value(${var});";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/array.pm b/lib/Catmandu/Fix/array.pm
index 1ac65dd..16ebd71 100644
--- a/lib/Catmandu/Fix/array.pm
+++ b/lib/Catmandu/Fix/array.pm
@@ -4,26 +4,15 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-
-            "if (is_hash_ref(${var})) {" .
-                "${var} = [\%{${var}}];" .
-            "}";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
 
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "if (is_hash_ref(${var})) {" .
+        "${var} = [\%{${var}}];" .
+    "}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/capitalize.pm b/lib/Catmandu/Fix/capitalize.pm
index 378c580..b9bb92d 100644
--- a/lib/Catmandu/Fix/capitalize.pm
+++ b/lib/Catmandu/Fix/capitalize.pm
@@ -4,22 +4,13 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "${var} = ucfirst(lc(as_utf8(${var}))) if is_string(${var});";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "${var} = ucfirst(lc(as_utf8(${var}))) if is_string(${var});";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/count.pm b/lib/Catmandu/Fix/count.pm
index 139adc9..35feb12 100644
--- a/lib/Catmandu/Fix/count.pm
+++ b/lib/Catmandu/Fix/count.pm
@@ -4,26 +4,17 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "if (is_array_ref(${var})) {" .
-                "${var} = scalar \@{${var}};" .
-            "} elsif (is_hash_ref(${var})) {" .
-                "${var} = scalar keys \%{${var}};" .
-            "}";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "if (is_array_ref(${var})) {" .
+        "${var} = scalar \@{${var}};" .
+    "} elsif (is_hash_ref(${var})) {" .
+        "${var} = scalar keys \%{${var}};" .
+    "}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/downcase.pm b/lib/Catmandu/Fix/downcase.pm
index 8233ecf..fc233ee 100644
--- a/lib/Catmandu/Fix/downcase.pm
+++ b/lib/Catmandu/Fix/downcase.pm
@@ -4,22 +4,13 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "${var} = lc(as_utf8(${var})) if is_string(${var});";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "${var} = lc(as_utf8(${var})) if is_string(${var});";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/from_json.pm b/lib/Catmandu/Fix/from_json.pm
index 5cf4b77..43d24a6 100644
--- a/lib/Catmandu/Fix/from_json.pm
+++ b/lib/Catmandu/Fix/from_json.pm
@@ -5,26 +5,21 @@ use JSON ();
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
+has _json_var => (is => 'rwp', writer => '_set_json_var', init_arg => undef);
+
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var, $fixer) = @_;
+
+    # memoize in case called multiple times
+    my $json_var = $self->_json_var ||
+                   $self->_set_json_var($fixer->capture(JSON->new->utf8(0)->pretty(0)->allow_nonref(1)));
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    my $json_var = $fixer->capture(JSON->new->utf8(0)->pretty(0)->allow_nonref(1));
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "if (is_string(${var})) {" .
-                "${var} = ${json_var}->decode(${var});" .
-            "}";
-        });
-    });
+    "if (is_string(${var})) {" .
+        "${var} = ${json_var}->decode(${var});" .
+    "}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/hash.pm b/lib/Catmandu/Fix/hash.pm
index 524af8d..cf0f525 100644
--- a/lib/Catmandu/Fix/hash.pm
+++ b/lib/Catmandu/Fix/hash.pm
@@ -4,26 +4,15 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-
-            "if (is_array_ref(${var})) {" .
-                "${var} = {\@{${var}}};" .
-            "}";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
 
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "if (is_array_ref(${var})) {" .
+        "${var} = {\@{${var}}};" .
+    "}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/join_field.pm b/lib/Catmandu/Fix/join_field.pm
index aa9ea42..e723a36 100644
--- a/lib/Catmandu/Fix/join_field.pm
+++ b/lib/Catmandu/Fix/join_field.pm
@@ -4,26 +4,18 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path      => (fix_arg => 1);
 has join_char => (fix_arg => 1, default => sub { '' });
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var, $fixer) = @_;
     my $join_char = $fixer->emit_string($self->join_char);
 
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "if (is_array_ref(${var})) {".
-                "${var} = join(${join_char}, grep { is_value(\$_) } \@{${var}});".
-            "}";
-        });
-    });
+    "if (is_array_ref(${var})) {".
+        "${var} = join(${join_char}, grep { is_value(\$_) } \@{${var}});".
+    "}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/prepend.pm b/lib/Catmandu/Fix/prepend.pm
index 3ca8971..36a1513 100644
--- a/lib/Catmandu/Fix/prepend.pm
+++ b/lib/Catmandu/Fix/prepend.pm
@@ -4,24 +4,15 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path  => (fix_arg => 1);
 has value => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-    my $value = $fixer->emit_string($self->value);
+with 'Catmandu::Fix::SimpleChangeValue';
 
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "${var} = join('', $value, ${var}) if is_value(${var});";
-        });
-    });
+sub emit_change_value {
+    my ($self, $var, $fixer) = @_;
+    my $value = $fixer->emit_string($self->value);
+    "${var} = join('', ${value}, ${var}) if is_value(${var});";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/replace_all.pm b/lib/Catmandu/Fix/replace_all.pm
index 3cfe9b6..b063592 100644
--- a/lib/Catmandu/Fix/replace_all.pm
+++ b/lib/Catmandu/Fix/replace_all.pm
@@ -4,29 +4,21 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path    => (fix_arg => 1);
 has search  => (fix_arg => 1);
 has replace => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
     my $search = $self->search;
     my $replace = $self->replace;
 
-    $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})) {"
-                ."utf8::upgrade(${var});"
-                ."${var} =~ s{$search}{$replace}g;"
-                ."}";
-        });
-    });
+    "if (is_value(${var})) {"
+        ."utf8::upgrade(${var});"
+        ."${var} =~ s{$search}{$replace}g;"
+        ."}";
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/trim.pm b/lib/Catmandu/Fix/trim.pm
index 217e700..5a5bf2d 100644
--- a/lib/Catmandu/Fix/trim.pm
+++ b/lib/Catmandu/Fix/trim.pm
@@ -4,32 +4,24 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 has mode => (fix_arg => 1, default => sub { 'whitespace' });
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            my $perl = "if (is_string(${var})) {";
-            if ($self->mode eq 'whitespace') {
-                $perl .= "${var} = trim(${var});";
-            }
-            if ($self->mode eq 'nonword') {
-                $perl .= $var.' =~ s/^\W+//;';
-                $perl .= $var.' =~ s/\W+$//;';
-            }
-            $perl .= "}";
-            $perl;
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
+
+    my $perl = "if (is_string(${var})) {";
+    if ($self->mode eq 'whitespace') {
+        $perl .= "${var} = trim(${var});";
+    }
+    if ($self->mode eq 'nonword') {
+        $perl .= $var.' =~ s/^\W+//;';
+        $perl .= $var.' =~ s/\W+$//;';
+    }
+    $perl .= "}";
+    $perl;
 }
 
 =head1 NAME
diff --git a/lib/Catmandu/Fix/upcase.pm b/lib/Catmandu/Fix/upcase.pm
index ff020be..dcc21fc 100644
--- a/lib/Catmandu/Fix/upcase.pm
+++ b/lib/Catmandu/Fix/upcase.pm
@@ -4,22 +4,13 @@ use Catmandu::Sane;
 use Moo;
 use Catmandu::Fix::Has;
 
-with 'Catmandu::Fix::Base';
-
 has path => (fix_arg => 1);
 
-sub emit {
-    my ($self, $fixer) = @_;
-    my $path = $fixer->split_path($self->path);
-    my $key = pop @$path;
-
-    $fixer->emit_walk_path($fixer->var, $path, sub {
-        my $var = shift;
-        $fixer->emit_get_key($var, $key, sub {
-            my $var = shift;
-            "${var} = uc(as_utf8(${var})) if is_string(${var});";
-        });
-    });
+with 'Catmandu::Fix::SimpleChangeValue';
+
+sub emit_change_value {
+    my ($self, $var) = @_;
+    "${var} = uc(as_utf8(${var})) if is_string(${var});";
 }
 
 =head1 NAME

-- 
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