diff --git a/lib/Catmandu/Fix/Bind.pm b/lib/Catmandu/Fix/Bind.pm index 872cb5ab9..3f3bef11d 100644 --- a/lib/Catmandu/Fix/Bind.pm +++ b/lib/Catmandu/Fix/Bind.pm @@ -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) = @_; @@ -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) = @_; @@ -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__}) { @@ -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); diff --git a/lib/Catmandu/Fix/Bind/Group.pm b/lib/Catmandu/Fix/Bind/Group.pm new file mode 100644 index 000000000..759dafc91 --- /dev/null +++ b/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 implementation as a L. +All fixes inside a Bind will be treated as one singular function. + +=head1 SEE ALSO + +L + +=cut diff --git a/lib/Catmandu/Fix/Bind/hashmap.pm b/lib/Catmandu/Fix/Bind/hashmap.pm index 3b8100df0..dea13ca50 100644 --- a/lib/Catmandu/Fix/Bind/hashmap.pm +++ b/lib/Catmandu/Fix/Bind/hashmap.pm @@ -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); diff --git a/lib/Catmandu/Fix/Bind/identity.pm b/lib/Catmandu/Fix/Bind/identity.pm index dc75bef87..108298345 100644 --- a/lib/Catmandu/Fix/Bind/identity.pm +++ b/lib/Catmandu/Fix/Bind/identity.pm @@ -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; diff --git a/lib/Catmandu/Fix/Bind/importer.pm b/lib/Catmandu/Fix/Bind/importer.pm index d6a40f339..0ab3ee966 100644 --- a/lib/Catmandu/Fix/Bind/importer.pm +++ b/lib/Catmandu/Fix/Bind/importer.pm @@ -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); diff --git a/lib/Catmandu/Fix/Bind/iterate.pm b/lib/Catmandu/Fix/Bind/iterate.pm index d73165d1a..f0e0536c5 100644 --- a/lib/Catmandu/Fix/Bind/iterate.pm +++ b/lib/Catmandu/Fix/Bind/iterate.pm @@ -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); diff --git a/lib/Catmandu/Fix/Bind/list.pm b/lib/Catmandu/Fix/Bind/list.pm index d420aa31b..dc0983cae 100644 --- a/lib/Catmandu/Fix/Bind/list.pm +++ b/lib/Catmandu/Fix/Bind/list.pm @@ -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); diff --git a/lib/Catmandu/Fix/Bind/visitor.pm b/lib/Catmandu/Fix/Bind/visitor.pm index a82f798d9..a895fc314 100644 --- a/lib/Catmandu/Fix/Bind/visitor.pm +++ b/lib/Catmandu/Fix/Bind/visitor.pm @@ -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'); diff --git a/lib/Catmandu/Fix/Bind/with.pm b/lib/Catmandu/Fix/Bind/with.pm index fed46e93a..6418b1063 100644 --- a/lib/Catmandu/Fix/Bind/with.pm +++ b/lib/Catmandu/Fix/Bind/with.pm @@ -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); @@ -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 @@ -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 diff --git a/t/Catmandu-Fix-Bind-Group.t b/t/Catmandu-Fix-Bind-Group.t new file mode 100644 index 000000000..cd4e37868 --- /dev/null +++ b/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;