Skip to content

Commit

Permalink
Adding support for 'library' fix
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Nov 18, 2017
1 parent 816ee2b commit 59ba477
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Build.PL
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.008.
# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.005.
use strict;
use warnings;

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ interactive mode:
...
fix >

Catmandu contains many powerful fixes. Visit [http://librecat.org/Catmandu/#fixes-cheat-sheet](http://librecat.org/Catmandu/#fixes-cheat-sheet) to get
Catmandu contains many powerful fixes. Visit [http://librecat.org/assets/catmandu\_cheat\_sheet.pdf](http://librecat.org/assets/catmandu_cheat_sheet.pdf) to get
an overview what is possible.

# Documentation
Expand Down
53 changes: 53 additions & 0 deletions lib/Catmandu/Fix/library.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package Catmandu::Fix::library;

use Moo;
use namespace::clean;
use Catmandu::Fix::Has;

with 'Catmandu::Fix::Base';

has module => (fix_arg => 1);

sub BUILD {
my ($self, $args) = @_;

my $pkg = Catmandu::Util::require_package($args->{module});

for my $module ($pkg->manifest) {
$module =~ s{^$pkg\::}{};
my $orig = $pkg . '::' . $module;
my $alias = 'Catmandu::Fix::' . $module;
Catmandu::Util::alias_package($orig,$alias);
}
}

sub emit {
my ($self, $fixer, $label) = @_;
"last ${label};";
}

1;

__END__
=pod
=head1 NAME
Catmandu::Fix::library - import fixes, conditions and binds from an external library
=head1 SYNOPSIS
# Import fixes methods from an external library
library("foobar")
# Use the methods from the 'foobar' library
foobar_method1()
foobar_method2()
...
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
5 changes: 0 additions & 5 deletions lib/Catmandu/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,6 @@ sub alias_package {
do {
# From Package::Alias by joshua@cpan.org & jpierce@cpan.org
no strict 'refs';

if (scalar keys %{$alias . "::" }) {
Catmandu::BadVal->throw("Cowardly refusing to alias over '$alias' because it's already in use");
}

*{$alias . "::"} = \*{$orig . "::"};
};

Expand Down
29 changes: 29 additions & 0 deletions t/Catmandu-Fix-library.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env perl
use lib 't/lib';
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu;
use utf8;

my $pkg;

BEGIN {
$pkg = 'Catmandu::Fix::library';
use_ok $pkg;
}

{
is_deeply $pkg->new('T::Foo::Bar')->fix({}), {},"added an external fix method";
}

{
my $fixer = Catmandu->fixer('library("T::Foo::Bar"); test(); if is_42(n) add_field(con,ok) end');

ok $fixer , 'got a fixer';

is_deeply $fixer->fix({n => '42'}) , { test => 'ok' , con => 'ok' , n => 42} , 'got the expected results';
}

done_testing 4;
11 changes: 11 additions & 0 deletions t/lib/T/Foo/Bar.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package T::Foo::Bar;
use Moo;

sub manifest {
qw(
T::Foo::Bar::test
T::Foo::Bar::Condition::is_42
);
}

1;
18 changes: 18 additions & 0 deletions t/lib/T/Foo/Bar/Condition/is_42.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package T::Foo::Bar::Condition::is_42;

use Catmandu::Sane;

use Moo;
use namespace::clean;
use Catmandu::Fix::Has;

has path => (fix_arg => 1);

with 'Catmandu::Fix::Condition::SimpleAllTest';

sub emit_test {
my ($self, $var) = @_;
"(${var} == 42)";
}

1;
12 changes: 12 additions & 0 deletions t/lib/T/Foo/Bar/test.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package T::Foo::Bar::test;
use Moo;

sub fix {
my ($self,$data) = @_;

$data->{test} = 'ok';

$data;
}

1;

0 comments on commit 59ba477

Please sign in to comment.