From: Vincent Pit Date: Tue, 23 Feb 2010 00:32:47 +0000 (+0100) Subject: Get rid of validate() X-Git-Tag: v0.07~26 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Frgit.git;a=commitdiff_plain;h=1c5bf56c6f80e4cd818c341b34ba3410f34ed514 Get rid of validate() --- diff --git a/lib/App/Rgit.pm b/lib/App/Rgit.pm index 1c8873d..ea9df10 100644 --- a/lib/App/Rgit.pm +++ b/lib/App/Rgit.pm @@ -5,7 +5,6 @@ use warnings; use App::Rgit::Command; use App::Rgit::Config; -use App::Rgit::Utils qw/validate/; =head1 NAME @@ -34,19 +33,25 @@ Creates a new L object that's bound to execute the command C<$cmd> on =cut sub new { - my ($class, %args) = &validate; + my $class = shift; + $class = ref $class || $class; + + my %args = @_; + my $config = App::Rgit::Config->new( root => $args{root}, git => $args{git}, debug => $args{debug}, ); return unless defined $config; + my $command = App::Rgit::Command->new( cmd => $args{cmd}, args => $args{args}, policy => $args{policy}, ); return unless defined $command; + bless { config => $config, command => $command, @@ -61,6 +66,7 @@ Actually run the commands. sub run { my $self = shift; + $self->command->run($self->config); } diff --git a/lib/App/Rgit/Command.pm b/lib/App/Rgit/Command.pm index 3a34822..348b8c4 100644 --- a/lib/App/Rgit/Command.pm +++ b/lib/App/Rgit/Command.pm @@ -5,7 +5,7 @@ use warnings; use Carp qw/croak/; -use App::Rgit::Utils qw/validate :codes/; +use App::Rgit::Utils qw/:codes/; =head1 NAME @@ -37,16 +37,23 @@ my %commands; __PACKAGE__->action($_ => 'Once') for qw/daemon gui help init version/, ' '; sub new { - my ($class, %args) = &validate; + my $class = shift; + $class = ref $class || $class; + + my %args = @_; + my $cmd = $args{cmd}; $cmd = ' ' unless defined $cmd; + my $action = $class->action($cmd); + if ($class eq __PACKAGE__) { $class = $action; } else { croak "Command $cmd should be executed as a $action" unless $class->isa($action); } + eval "require $action; 1" or croak "Couldn't load $action: $@"; bless { cmd => $cmd, diff --git a/lib/App/Rgit/Config.pm b/lib/App/Rgit/Config.pm index 76a6050..1cac51d 100644 --- a/lib/App/Rgit/Config.pm +++ b/lib/App/Rgit/Config.pm @@ -8,7 +8,7 @@ use Cwd qw/abs_path/; use File::Spec::Functions qw/file_name_is_absolute/; use App::Rgit::Repository; -use App::Rgit::Utils qw/validate :levels/; +use App::Rgit::Utils qw/:levels/; use constant IS_WIN32 => $^O eq 'MSWin32'; @@ -39,7 +39,10 @@ Creates a new configuration object based on the root directory C<$root> and usin =cut sub new { - my ($class, %args) = &validate; + my $class = shift; + $class = ref $class || $class; + + my %args = @_; my $root = $args{root}; return unless defined $root and -d $root; diff --git a/lib/App/Rgit/Repository.pm b/lib/App/Rgit/Repository.pm index fd19bab..1d8d490 100644 --- a/lib/App/Rgit/Repository.pm +++ b/lib/App/Rgit/Repository.pm @@ -14,8 +14,6 @@ BEGIN { *WIFSIGNALED = sub { shift() & 127 } unless eval { WIFSIGNALED(0); 1 }; } -use App::Rgit::Utils qw/validate/; - =head1 NAME App::Rgit::Repository - Class representing a Git repository. @@ -44,10 +42,15 @@ If the C option is passed, C<$dir> isn't checked to be a valid C repo =cut sub new { - my ($class, %args) = &validate; + my $class = shift; + $class = ref $class || $class; + + my %args = @_; + my $dir = $args{dir}; $dir = abs_path $dir if defined $dir and not file_name_is_absolute $dir; $dir = cwd unless defined $dir; + my ($repo, $bare, $name, $work); if ($args{fake}) { $repo = $work = $dir; @@ -77,6 +80,7 @@ sub new { $work = $repo; } } + bless { fake => !!$args{fake}, repo => $repo, diff --git a/lib/App/Rgit/Utils.pm b/lib/App/Rgit/Utils.pm index 7a4c3b3..cfe191d 100644 --- a/lib/App/Rgit/Utils.pm +++ b/lib/App/Rgit/Utils.pm @@ -51,26 +51,8 @@ use constant { CRIT => 0, }; -=head1 FUNCTIONS - -=head2 C - -Sanitize arguments passed to methods. - -=cut - -sub validate { - my $class = shift; - croak 'Optional arguments must be passed as key/value pairs' if @_ % 2; - $class = ref($class) || $class; - $class = caller unless $class; - return $class, @_; -} - =head1 EXPORT -C is only exported on request, either by its name or by the C<'funcs'> tag. - C C, C and C are only exported on request, either by their name or by the C<'codes'> tags. C, C, C and C are only exported on request, either by their name or by the C<'levels'> tags. @@ -81,7 +63,6 @@ use base qw/Exporter/; our @EXPORT = (); our %EXPORT_TAGS = ( - funcs => [ qw/validate/ ], codes => [ qw/SAVE NEXT REDO LAST/ ], levels => [ qw/INFO WARN ERR CRIT/ ], ); diff --git a/t/15-failures.t b/t/15-failures.t index 296ffc8..3217e23 100644 --- a/t/15-failures.t +++ b/t/15-failures.t @@ -6,15 +6,12 @@ use warnings; use Cwd qw/cwd/; use File::Spec::Functions qw/catdir/; -use Test::More tests => 45; +use Test::More tests => 42; use App::Rgit; local $SIG{__WARN__} = sub { die @_ }; -eval { App::Rgit->new(qw/foo bar baz/) }; -like($@, qr!Optional\s+arguments\s+must\s+be\s+passed\s+as\s+keys?\s*/\s*values?\s+pairs?!, 'App::Rgit->new(even): croaks'); - my $res = eval { App::Rgit->new() }; is($@, '', 'App::Rgit->new(): no root: does not croak'); is($res, undef, 'App::Rgit->new(): no root: returns undef'); @@ -43,10 +40,6 @@ $res = eval { $res->new(root => 't', git => 't/bin/git', cmd => 'version'); }; is($@, '', '$ar->new(): no args: does not croak'); isa_ok($res, 'App::Rgit', '$ar->new(): no args: returns an object'); -$res = eval { App::Rgit::new(undef, root => 't', git => 't/bin/git', cmd => 'version'); }; -is($@, '', 'undef->App::Rgit::new(): no args: does not croak'); -isa_ok($res, 'App::Rgit','undef->App::Rgit::new(): no args: returns an object'); - use App::Rgit::Command; eval { App::Rgit::Command::Once->App::Rgit::Command::new(cmd => 'dongs') };