Commit | Line | Data |
16585083 |
1 | package Users; |
2 | use strict; |
3 | use warnings; |
4 | use base qw/Class::Accessor/; |
5 | |
6 | __PACKAGE__->mk_accessors(qw/username password/); |
7 | |
8 | sub new { |
9 | my ($class, %p) = @_; |
10 | bless { %p }, $class; |
11 | } |
12 | |
13 | package TestApp::Model::Tangram; |
14 | use strict; |
15 | use warnings; |
16 | use base qw/Catalyst::Model/; |
17 | use DBI; |
18 | use Tangram::Relational; |
19 | use Tangram::Storage; |
20 | use Tangram::Type::String; |
21 | use Class::C3; |
22 | use File::Temp qw/tempfile/; |
23 | |
24 | BEGIN { |
25 | __PACKAGE__->mk_accessors(qw/storage schema _sqlite_file/); |
26 | } |
27 | |
28 | sub COMPONENT { |
29 | my ($class, $app, @rest) = @_; |
30 | my $self = $class->next::method($app, @rest); |
31 | my ($fh, $fn) = tempfile; |
32 | close($fh); |
33 | $self->{_sqlite_file} = $fn; |
34 | my @dsn = ("DBI:SQLite:dbname=$fn", '', ''); |
35 | my $dbh = DBI->connect(@dsn); |
36 | $self->{schema} = Tangram::Relational->schema( { |
37 | classes => [ |
38 | Users => { |
39 | fields => { |
40 | string => [qw/username password/], |
41 | }, |
42 | }, |
43 | ], |
44 | }); |
45 | Tangram::Relational->deploy($self->schema, $dbh); |
46 | $dbh->disconnect; |
47 | $self->{storage} = Tangram::Relational->connect( |
48 | $self->schema, @dsn |
49 | ); |
50 | my $test_user = Users->new(username => 'testuser', password => 'testpass'); |
51 | $self->storage->insert($test_user); |
52 | return $self; |
53 | } |
54 | |
55 | sub DESTROY { |
56 | my ($self) = @_; |
57 | $self->storage->disconnect if $self->storage; |
58 | unlink $self->{_sqlite_file}; |
59 | } |
60 | |
61 | 1; |
62 | |