From ca2d44ddfeb31f9169577db0f2a8b4d15ed46e13 Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Thu, 9 Oct 2014 15:56:06 -0600 Subject: [PATCH] More command-line testing tools: get body by name/id --- lib/Lacuna/DB.pm | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/Lacuna/DB.pm b/lib/Lacuna/DB.pm index da734907..3dff35ff 100644 --- a/lib/Lacuna/DB.pm +++ b/lib/Lacuna/DB.pm @@ -14,34 +14,50 @@ 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) = @_; +sub _where +{ + my ($self, $type, $id) = @_; - my $empires = $self->resultset('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 }; + $where{$type} = { like => $id }; } # or just the name (hopefully no names are purely numeric) elsif ($id =~ /\D/) { - $where{name} = $id; + $where{$type} = $id; } # or the numeric ID (probably most useful for testing) else { $where{id} = $id; } + \%where; +} + +# 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('Empire'); + my $where = $self->_where(name => $id); + + $empires->find($where)->single; +} + +sub body { + my ($self, $id) = @_; + + my $bodies = $self->resultset('Map::Body'); + my $where = $self->_where(name => $id); - $empires->find(\%where); + $bodies->find($where); } # similarly, a lot of typing can be saved with Lacuna->db->building($id) -- 2.51.2