Skip to content

Commit

Permalink
Adding Catmandu::Fix::Bind::Group as role
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed May 29, 2017
1 parent f99d869 commit c35702f
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 19 deletions.
9 changes: 4 additions & 5 deletions lib/Catmandu/Fix/Bind.pm
Expand Up @@ -15,7 +15,6 @@ requires 'bind';

has __return__ => (is => 'rw', default => sub {[0]});
has __fixes__ => (is => 'rw', default => sub {[]});
has __group__ => (is => 'ro', default => sub {0});

around bind => sub {
my ($orig, $self, $prev, @args) = @_;
Expand Down Expand Up @@ -57,7 +56,7 @@ sub emit {
$fix_stash->add_symbol('&emit_reject' => sub { "return ${bind_var}->reject(${var});"; });
}
# Allow Bind-s to bind to all fixes in if-unless-else statements
if ($self->__group__ == 0) {
unless ($self->does('Catmandu::Fix::Bind::Group')) {
$fix_emit_fixes = $fix_stash->get_symbol('&emit_fixes');
$fix_stash->add_symbol('&emit_fixes' => sub {
my ($this, $fixes) = @_;
Expand Down Expand Up @@ -89,8 +88,8 @@ sub emit {

$perl .= "my ${unit} = ${bind_var}->unit(${var});";

# If __group__ is set, then all fixes are executed as one block in a bind
if ($self->__group__) {
# If this is a Bind::Group, then all fixes are executed as one block in a bind
if ($self->does("Catmandu::Fix::Bind::Group")) {
my $generated_code = "sub { my ${var} = shift;";

for my $fix (@{$self->__fixes__}) {
Expand All @@ -102,7 +101,7 @@ sub emit {

$perl .= "${unit} = ${bind_var}->bind(${unit}, $generated_code);";
}
# If __group__ isn't set, then bind will be executed for each seperate fix
# If this isn't a Bind::Group, then bind will be executed for each seperate fix
else {
for my $fix (@{$self->__fixes__}) {
my $name = ref($fix);
Expand Down
47 changes: 47 additions & 0 deletions lib/Catmandu/Fix/Bind/Group.pm
@@ -0,0 +1,47 @@
package Catmandu::Fix::Bind::Group;

use Catmandu::Sane;

our $VERSION = '1.0507';

use Moo::Role;
use namespace::clean;

1;

__END__
=pod
=head1 NAME
Catmandu::Fix::Bind::Group - a role for a binder that executes all fixes as one group
=head1 SYNOPSIS
# Fixes fix1(), ... fixN() will be passed as one function to the internal 'bind' method
do identity()
fix1()
.
.
fixN()
end
# Fixes fix1(), ... fixN() will be passed as one by one to the internal 'bind' method
do maybe()
fix1()
.
.
fixN()
end
=head1 DESCRIPTION
This role flags a L<Catmandu::Fix::Bind> implementation as a L<Catmandu::Fix::Bind::Group>.
All fixes inside a Bind will be treated as one singular function.
=head1 SEE ALSO
L<Catmandu::Fix::Bind>
=cut
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Bind/hashmap.pm
Expand Up @@ -9,7 +9,7 @@ use Catmandu::Util qw(:is);
use namespace::clean;
use Catmandu::Fix::Has;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has exporter => (fix_opt => 1);
has store => (fix_opt => 1);
Expand Down
7 changes: 1 addition & 6 deletions lib/Catmandu/Fix/Bind/identity.pm
Expand Up @@ -7,12 +7,7 @@ our $VERSION = '1.0507';
use Moo;
use namespace::clean;

with 'Catmandu::Fix::Bind';

sub BUILD {
my ($self) = @_;
$self->{__group__} = 1;
}
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

1;

Expand Down
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Bind/importer.pm
Expand Up @@ -9,7 +9,7 @@ use Catmandu::Util qw(:is);
use namespace::clean;
use Catmandu::Fix::Has;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has importer_name => (fix_arg => 1);
has step => (fix_opt => 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Bind/iterate.pm
Expand Up @@ -9,7 +9,7 @@ use Catmandu::Util;
use Catmandu::Fix::Has;
use namespace::clean;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has start => (fix_opt => 1);
has end => (fix_opt => 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Bind/list.pm
Expand Up @@ -10,7 +10,7 @@ use Catmandu::Util;
use namespace::clean;
use Catmandu::Fix::Has;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has path => (fix_opt => 1);
has var => (fix_opt => 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Bind/visitor.pm
Expand Up @@ -8,7 +8,7 @@ use Moo;
use Catmandu::Util;
use namespace::clean;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has path => (is => 'ro');

Expand Down
6 changes: 3 additions & 3 deletions lib/Catmandu/Fix/Bind/with.pm
Expand Up @@ -10,7 +10,7 @@ use Catmandu::Util;
use namespace::clean;
use Catmandu::Fix::Has;

extends 'Catmandu::Fix::Bind::identity';
with 'Catmandu::Fix::Bind' , 'Catmandu::Fix::Bind::Group';

has path => (fix_opt => 1);

Expand Down Expand Up @@ -84,7 +84,7 @@ Catmandu::Fix::Bind::with - a binder that computes Fix-es in the context of a pa
- name: nicolas
# Fix
do with(path => data)
do with(path:data)
if all_match(name,nicolas)
reject()
end
Expand All @@ -110,7 +110,7 @@ these two fixes are equal:
add_field(my.deep.field.style, funk)
do with(path => my.deep.field)
do with(path:my.deep.field)
add_field(style,funk)
end
Expand Down
16 changes: 16 additions & 0 deletions t/Catmandu-Fix-Bind-Group.t
@@ -0,0 +1,16 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Util qw(:is);

my $pkg;

BEGIN {
$pkg = 'Catmandu::Fix::Bind::Group';
use_ok $pkg;
}
require_ok $pkg;

done_testing 2;

0 comments on commit c35702f

Please sign in to comment.