Skip to content

Instantly share code, notes, and snippets.

@nicomen
Last active December 6, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomen/c749c349ed0a74799edc68fc9c9ffea5 to your computer and use it in GitHub Desktop.
Save nicomen/c749c349ed0a74799edc68fc9c9ffea5 to your computer and use it in GitHub Desktop.
Simple db proxy class
package Supplier;
use strict;
use warnings;
use Moo;
use Carp qw/croak/;
has 'code' => ( is => 'ro', required => 1 );
has 'db_row' => ( is => 'lazy', builder => 1 );
for my $attr (qw/name id active group_id/) {
has $attr => ( is => 'lazy', default => sub { shift->db_row->{$attr}; } );
}
with 'Role::WithDBModel';
sub _build_db_row {
my $self = shift;
my $db_row = $self->model( 'supplier' )->get_supplier_by_code( $self->code );
croak "Couldn't find supplier " . $self->code . " in database" unless $db_row;
return $db_row;
}
=pod
Simple skeleton for a supplier
=cut
1;
@nicomen
Copy link
Author

nicomen commented Dec 6, 2017

Usage: Supplier->new( code => "CODE" )->name; (would fetch the name of a supplier with a certain code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment