Last active
December 6, 2017 13:31
-
-
Save nicomen/c749c349ed0a74799edc68fc9c9ffea5 to your computer and use it in GitHub Desktop.
Simple db proxy class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: Supplier->new( code => "CODE" )->name; (would fetch the name of a supplier with a certain code)