Created
February 4, 2017 12:02
-
-
Save kirklewis/20299ba9efb6ed708b1550e9b6ac1636 to your computer and use it in GitHub Desktop.
learn-orm-perl-with-dbix
This file contains 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
#!/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; |
This file contains 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
#!/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); |
This file contains 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
sub fullname { | |
my $self = shift; | |
return $self->firstname . ' ' . $self->lastname; | |
} |
This file contains 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 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