diff --git a/lib/Geo/Coder/Free/DB.pm b/lib/Geo/Coder/Free/DB.pm index 318bf608..7b174cda 100644 --- a/lib/Geo/Coder/Free/DB.pm +++ b/lib/Geo/Coder/Free/DB.pm @@ -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. @@ -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 @@ -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; diff --git a/lib/Geo/Coder/Free/MaxMind.pm b/lib/Geo/Coder/Free/MaxMind.pm index a62c440a..54150e4a 100644 --- a/lib/Geo/Coder/Free/MaxMind.pm +++ b/lib/Geo/Coder/Free/MaxMind.pm @@ -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; diff --git a/t/00-load.t b/t/00-load.t index fe49413d..0d4631bc 100644 --- a/t/00-load.t +++ b/t/00-load.t @@ -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");