Skip to content

Instantly share code, notes, and snippets.

@kirklewis
Created February 4, 2017 12:02
Show Gist options
  • Save kirklewis/20299ba9efb6ed708b1550e9b6ac1636 to your computer and use it in GitHub Desktop.
Save kirklewis/20299ba9efb6ed708b1550e9b6ac1636 to your computer and use it in GitHub Desktop.
learn-orm-perl-with-dbix
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use App::Schema;
use_ok('App::Scema');
my $schema = App::Schema->connect('dbi:SQLite:app.db');
my $user_rs = $schema->resultset('User');
# Check custom accessors are defined
can_ok($user_rs->result_class, qw(fullname));
is($user_rs->find(1)->fullname,
'Bob Doe', 'Should read from set using custom accessor');
# Check custom methods are defined
can_ok($user_rs, qw(age_less_than));
is($user_rs->age_less_than(50)->fullname,
'Sarah Connor', 'Should perform search using custom method');
done_testing;
#!/usr/bin/env perl
use strict;
use warnings;
use DBIx::Class::Schema::Loader qw(make_schema_at);
my @dsn = 'dbi:SQLite:dbname=app.db';
my %options = (
dump_directory => './lib'
);
make_schema_at('App::Schema' => \%options, \@dsn);
sub fullname {
my $self = shift;
return $self->firstname . ' ' . $self->lastname;
}
package App::Schema::ResultSet::User;
use base 'DBIx::Class::ResultSet';
sub age_less_than {
my $self = shift;
my $age = shift;
return $self->find({ age => { '<' => $age }});
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment