Skip to content

Commit

Permalink
Latest DB.pm from NJH-Snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Oct 14, 2023
1 parent 10dcace commit dd9b70f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
34 changes: 24 additions & 10 deletions lib/Geo/Coder/Free/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,29 @@ use Carp;
our $directory;
our $logger;
our $cache;
our $cache_duration;

=head1 SUBROUTINES/METHODS
=head2 init
Set some class level defaults.
__PACKAGE__::DB::init(directory => '../databases')
See the documentation for new() to see what variables can be set
=cut

sub init {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

$directory ||= $args{'directory'};
$logger ||= $args{'logger'};
$cache ||= $args{'cache'};
$cache_duration ||= $args{'cache_duration'};
}

=head2 new
Create an object to point to a read-only database.
Expand All @@ -76,6 +96,9 @@ Arguments:
cache => place to store results
cache_duration => how long to store results in the cache (default is 1 hour)
directory => where the database file is held
If the arguments are not set, tries to take from class level defaults
=cut

Expand All @@ -96,21 +119,12 @@ sub new {
logger => $args{'logger'} || $logger,
directory => $args{'directory'} || $directory, # The directory containing the tables in XML, SQLite or CSV format
cache => $args{'cache'} || $cache,
cache_duration => $args{'cache_duration'} || '1 hour',
cache_duration => $args{'cache_duration'} || $cache_duration || '1 hour',
table => $args{'table'}, # The name of the file containing the table, defaults to the class name
no_entry => $args{'no_entry'} || 0,
}, $class;
}

# Can also be run as a class level __PACKAGE__::DB::init(directory => '../databases')
sub init {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

$directory ||= $args{'directory'};
$logger ||= $args{'logger'};
$cache ||= $args{'cache'};
}

sub set_logger {
my $self = shift;

Expand Down
25 changes: 19 additions & 6 deletions lib/Geo/Coder/Free/MaxMind.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,33 @@ The admin2.db is far from comprehensive, see Makefile.PL for some entries that a
=cut

sub new {
my($proto, %param) = @_;
my $class = ref($proto) || $proto;
my $class = $_[0];

# Use Geo::Coder::Free->new, not Geo::Coder::Free::new
return unless($class);
shift;
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

if(!defined($class)) {
# Use Geo::Coder::Free->new, not Geo::Coder::Free::new
# carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
# return;

# FIXME: this only works when no arguments are given
$class = __PACKAGE__;
} elsif(ref($class)) {
# clone the given object
return bless { %{$class}, %args }, ref($class);
}

# Geo::Coder::Free::DB::init(directory => 'lib/Geo/Coder/Free/databases');

my $directory = $param{'directory'} || Module::Info->new_from_loaded(__PACKAGE__)->file();
my $directory = $args{'directory'} || Module::Info->new_from_loaded(__PACKAGE__)->file();
$directory =~ s/\.pm$//;

Geo::Coder::Free::DB::init({
cache_duration => '1 day',
%args,
directory => File::Spec->catfile($directory, 'databases'),
cache => $param{cache} || CHI->new(driver => 'Memory', datastore => {})
cache => $args{cache} || CHI->new(driver => 'Memory', datastore => {}),
});

return bless { }, $class;
Expand Down
4 changes: 2 additions & 2 deletions t/00-load.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ BEGIN {
}
}

diag("Testing Geo::Coder::Free $Geo::Coder::Free::VERSION, Perl $], $^X");

foreach my $module(@modules) {
require_ok($module) || print 'Bail out!';
}

diag("Testing Geo::Coder::Free $Geo::Coder::Free::VERSION, Perl $], $^X");

0 comments on commit dd9b70f

Please sign in to comment.