Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource interface #1212

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/Rex/Function/Common.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# (c) Jan Gehring <[email protected]>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::Function::Common;

use strict;
use warnings;

# VERSION

require Exporter;
require Rex::Config;
use Data::Dumper;
use base qw(Exporter);
use vars qw(@EXPORT);
use MooseX::Params::Validate;
use Rex::MultiSub::Function;

@EXPORT = qw(function);

sub function {
my ( $name, $options, $function ) = @_;
my $name_save = $name;
if ( $name_save !~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ ) {
Rex::Logger::info(
"Please use only the following characters for function names:", "warn" );
Rex::Logger::info( " A-Z, a-z, 0-9 and _", "warn" );
Rex::Logger::info( "Also the function should start with A-Z or a-z",
"warn" );
die "Wrong function name syntax.";
}

my $sub = Rex::MultiSub::Function->new(
name => $name_save,
function => $function,
params_list => $options->{params_list},
test_wantarray => 1,
);

my ( $class, $file, @tmp ) = caller;

$sub->export( $class, $options->{export} );
}

1;
115 changes: 115 additions & 0 deletions lib/Rex/MultiSub.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#
# (c) Jan Gehring <[email protected]>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::MultiSub;

use strict;
use warnings;

# VERSION

use Moose;
use MooseX::Params::Validate;
use Rex::MultiSub::LookupTable;

use Data::Dumper;
use Carp;

has methods => (
is => 'rw',
isa => 'Rex::MultiSub::LookupTable',
default => sub {
Rex::MultiSub::LookupTable->instance;
}
);

has name => ( is => 'ro', isa => 'Str' );
has function => ( is => 'ro', isa => 'CodeRef' );
has params_list => ( is => 'ro', isa => 'ArrayRef' );

sub validate { }
sub error { }

sub BUILD {
my ($self) = @_;
$self->methods->add( $self->name, $self->params_list, $self->function );
}

sub export {
my ( $self, $ns, $global ) = @_;

my $name = $self->name;

no strict 'refs';
no warnings 'redefine';

*{"${ns}::$name"} = sub {
$self->dispatch(@_);
};

if ($global) {

# register in caller namespace
push @{ $ns . "::ISA" }, "Rex::Exporter"
unless ( grep { $_ eq "Rex::Exporter" } @{ $ns . "::ISA" } );
push @{ $ns . "::EXPORT" }, $self->name;
}

use strict;
use warnings;
}

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

my @errors;
my $exec;
my @all_args;
my $found = 0;

for my $f (
sort {
scalar( @{ $b->{params_list} } ) <=>
scalar( @{ $a->{params_list} } )
} @{ $self->methods->data->{ $self->name } }
)
{

eval {
@all_args = $self->validate( $f, @args );
$found = 1;
1;
} or do {
push @errors, $@;

# print "Err: $@\n";
# TODO catch no "X parameter was given" errors
next;
};

$exec = $f->{code};

last;
}
if ( !$found ) {
my @err_msg;
for my $err (@errors) {
my ($fline) = split( /\n/, $err );
push @err_msg, $fline;
}

$self->error(@err_msg);
}

$self->call( $exec, @all_args );
}

sub call {
my ( $self, $code, @args ) = @_;
$code->(@args);
}

1;
46 changes: 46 additions & 0 deletions lib/Rex/MultiSub/Function.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# (c) Jan Gehring <[email protected]>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::MultiSub::Function;

use strict;
use warnings;

# VERSION

use Moose;
use MooseX::Params::Validate;
use Rex::MultiSub::LookupTable;

use Data::Dumper;
use Carp;

extends qw(Rex::MultiSub::PosValidatedList);

has test_wantarray => (
is => 'ro',
isa => 'Bool',
default => sub { 0 },
);

override call => sub {
my ( $self, $code, @args ) = @_;

my $ret = $code->(@args);

if ( exists $ret->{value} ) {
if ( $self->test_wantarray && wantarray ) {
return split( /\n/, $ret->{value} );
}
else {
return $ret->{value};
}
}

return undef;
};

1;
31 changes: 31 additions & 0 deletions lib/Rex/MultiSub/LookupTable.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# (c) Jan Gehring <[email protected]>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::MultiSub::LookupTable;

use strict;
use warnings;

# VERSION

use MooseX::Singleton;

has data => (
is => 'ro',
isa => 'HashRef',
default => sub { {} },
);

sub add {
my ( $self, $name, $params_list, $code ) = @_;
push @{ $self->data->{$name} },
{
params_list => $params_list,
code => $code,
};
}

1;
47 changes: 47 additions & 0 deletions lib/Rex/MultiSub/PosValidatedList.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# (c) Jan Gehring <[email protected]>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::MultiSub::PosValidatedList;

use strict;
use warnings;

# VERSION

use Moose;
use MooseX::Params::Validate;
use Rex::MultiSub::LookupTable;

use Data::Dumper;
use Carp;

extends qw(Rex::MultiSub);

override validate => sub {
my ( $self, $func_opts, @args ) = @_;

my @_x = @{ $func_opts->{params_list} };
my @order = map { $_x[$_] } grep { $_ & 1 } 1 .. $#_x;

my @v_args = pos_validated_list(
\@args, @order,
MX_PARAMS_VALIDATE_NO_CACHE => 1,
MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1
);

return @v_args;
};

override error => sub {
my ( $self, @err_msg ) = @_;

my $name = $self->name;
croak "Function $name for provided parameter not found.\nErrors:\n"
. join( "\n", @err_msg );
};

1;

Loading