From: Peter Rabbitson Date: Mon, 30 Aug 2010 03:17:35 +0000 (+0200) Subject: Minor cleanup of ::Optional::Dependencies innards X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d8799bab98fa442f681074ece8008a3eefea357c;p=dbsrgits%2FDBIx-Class-Historic.git Minor cleanup of ::Optional::Dependencies innards --- diff --git a/lib/DBIx/Class/Optional/Dependencies.pm b/lib/DBIx/Class/Optional/Dependencies.pm index 5cb513b..6d4ed6f 100644 --- a/lib/DBIx/Class/Optional/Dependencies.pm +++ b/lib/DBIx/Class/Optional/Dependencies.pm @@ -215,9 +215,7 @@ sub req_ok_for { croak "req_ok_for() expects a requirement group name" unless $group; - $class->_check_deps ($group) unless $req_availability_cache{$group}; - - return $req_availability_cache{$group}{status}; + return $class->_check_deps($group)->{status}; } sub req_missing_for { @@ -226,9 +224,7 @@ sub req_missing_for { croak "req_missing_for() expects a requirement group name" unless $group; - $class->_check_deps ($group) unless $req_availability_cache{$group}; - - return $req_availability_cache{$group}{missing}; + return $class->_check_deps($group)->{missing}; } sub req_errorlist_for { @@ -237,44 +233,49 @@ sub req_errorlist_for { croak "req_errorlist_for() expects a requirement group name" unless $group; - $class->_check_deps ($group) unless $req_availability_cache{$group}; - - return $req_availability_cache{$group}{errorlist}; + return $class->_check_deps($group)->{errorlist}; } sub _check_deps { my ($class, $group) = @_; - my $deps = $class->req_list_for ($group); + return $req_availability_cache{$group} ||= do { + + my $deps = $class->req_list_for ($group); + + my %errors; + for my $mod (keys %$deps) { + my $req_line = "require $mod;"; + if (my $ver = $deps->{$mod}) { + $req_line .= "$mod->VERSION($ver);"; + } + + eval $req_line; + + $errors{$mod} = $@ if $@; + } + + my $res; - my %errors; - for my $mod (keys %$deps) { - if (my $ver = $deps->{$mod}) { - eval "use $mod $ver ()"; + if (keys %errors) { + my $missing = join (', ', map { $deps->{$_} ? "$_ >= $deps->{$_}" : $_ } (sort keys %errors) ); + $missing .= " (see $class for details)" if $reqs->{$group}{pod}; + $res = { + status => 0, + errorlist => \%errors, + missing => $missing, + }; } else { - eval "require $mod"; + $res = { + status => 1, + errorlist => {}, + missing => '', + }; } - $errors{$mod} = $@ if $@; - } - - if (keys %errors) { - my $missing = join (', ', map { $deps->{$_} ? "$_ >= $deps->{$_}" : $_ } (sort keys %errors) ); - $missing .= " (see $class for details)" if $reqs->{$group}{pod}; - $req_availability_cache{$group} = { - status => 0, - errorlist => { %errors }, - missing => $missing, - }; - } - else { - $req_availability_cache{$group} = { - status => 1, - errorlist => {}, - missing => '', - }; - } + $res; + }; } sub req_group_list { @@ -310,7 +311,7 @@ sub _gen_pod { or die "Hrmm? No sqlt dep?"; my @chunks = ( - <<"EOC", + <<'EOC', ######################################################################### ##################### A U T O G E N E R A T E D ######################## ######################################################################### @@ -323,7 +324,7 @@ EOC '=head1 NAME', "$class - Optional module dependency specifications (for module authors)", '=head1 SYNOPSIS', - <'s Makefile.PL): ... @@ -385,10 +386,10 @@ EOD '=head1 METHODS', '=head2 req_group_list', '=over', - '=item Arguments: $none', + '=item Arguments: none', '=item Returns: \%list_of_requirement_groups', '=back', - < version of DBIx::Class. See the L for a real-world @@ -411,14 +412,17 @@ EOD '=item Arguments: $group_name', '=item Returns: 1|0', '=back', - 'Returns true or false depending on whether all modules required by C<$group_name> are present on the system and loadable', + <<'EOD', +Returns true or false depending on whether all modules required by +C<$group_name> are present on the system and loadable. +EOD '=head2 req_missing_for', '=over', '=item Arguments: $group_name', '=item Returns: $error_message_string', '=back', - <req_ok_for ('deploy'), - 'deploy() deps missing', -); - -like ( - DBIx::Class::Optional::Dependencies->req_missing_for ('deploy'), - qr/^SQL::Translator \>\= \d/, - 'expected missing string contents', -); - -like ( - DBIx::Class::Optional::Dependencies->req_errorlist_for ('deploy')->{'SQL::Translator'}, - qr/Optional Dep Test/, - 'custom exception found in errorlist', -); - +{ + local @INC = (sub { die('Optional Dep Test') } ); + + ok ( + ! DBIx::Class::Optional::Dependencies->req_ok_for ('deploy'), + 'deploy() deps missing', + ); + + like ( + DBIx::Class::Optional::Dependencies->req_missing_for ('deploy'), + qr/^SQL::Translator \>\= \d/, + 'expected missing string contents', + ); + + like ( + DBIx::Class::Optional::Dependencies->req_errorlist_for ('deploy')->{'SQL::Translator'}, + qr/Optional Dep Test/, + 'custom exception found in errorlist', + ); +} #make it so module appears loaded $INC{'SQL/Translator.pm'} = 1; @@ -83,5 +84,4 @@ is_deeply ( 'expected empty errorlist', ); - done_testing;