From 97aec72e4d37690d106e897d20c95987139b50ca Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Thu, 26 Jun 2014 19:59:04 -0600 Subject: [PATCH] Create a couple of short-cut methods to make testing via one-liners easier: Lacuna->db->empire(name_or_id) and Lacuna->db->building(id). This may also be useful for new scripts, whether AI or otherwise. --- lib/Lacuna/DB.pm | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/Lacuna/DB.pm b/lib/Lacuna/DB.pm index 09760447..b819ce89 100644 --- a/lib/Lacuna/DB.pm +++ b/lib/Lacuna/DB.pm @@ -14,5 +14,44 @@ sub sqlt_deploy_hook { $sqlt_schema->drop_table('noexist_map'); } +# Simplifies many scripts, especially one-liners, for testing purposes as well +# as for the bin scripts: +# perl -MLacuna -E '$e=Lacuna->db->empire(1); say $e->id, ": ", $e->name' +# 1: Lacuna Expanse Corp +sub empire { + my ($self, $id) = @_; + my $empires = $self->resultset('Lacuna::DB::Result::Empire'); + my %where; + + # set the where hash + if (ref $id && ref $id eq 'HASH') + { + %where = %$id; + } + # or a C for name + elsif ($id =~ /[%_]/) + { + $where{name} = { like => $id }; + } + # or just the name (hopefully no names are purely numeric) + elsif ($id =~ /\D/) + { + $where{name} = $id; + } + # or the numeric ID (probably most useful for testing) + else + { + $where{id} = $id; + } + + $empires->find(\%where); +} + +# similarly, a lot of typing can be saved with Lacuna->db->building($id) +sub building { + my ($self, $building_id) = @_; + my $building = $self->resultset('Lacuna::DB::Result::Building')->find($building_id); +} + no Moose; __PACKAGE__->meta->make_immutable; -- 2.54.0