File: | lib/Railsish/Database.pm |
Coverage: | 55.1% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Railsish::Database; | ||||||
2 | # ABSTRACT: Talks to database | ||||||
3 | |||||||
4 | 2 2 2 | 149 11 22 | use Any::Moose; | ||||
5 | 2 2 2 | 155 11 34 | use KiokuDB; | ||||
6 | 2 2 2 | 172 7 28 | use Railsish::CoreHelpers; | ||||
7 | 2 2 2 | 199 8 17 | use YAML::Any qw(LoadFile); | ||||
8 | |||||||
9 | has 'config' => ( | ||||||
10 | is => "ro", | ||||||
11 | isa => "HashRef", | ||||||
12 | lazy_build => 1, | ||||||
13 | required => 1 | ||||||
14 | ); | ||||||
15 | |||||||
16 | has 'dsn' => ( | ||||||
17 | isa => "Str", | ||||||
18 | is => "rw", | ||||||
19 | lazy_build => 1 | ||||||
20 | ); | ||||||
21 | |||||||
22 | has 'kioku' => ( | ||||||
23 | is => "rw", | ||||||
24 | lazy_build => 1 | ||||||
25 | ); | ||||||
26 | |||||||
27 | sub _build_config { | ||||||
28 | 2 | 6 | my $self = shift; | ||||
29 | 2 | 12 | my $file = app_root(config => "database.yml"); | ||||
30 | |||||||
31 | 2 | 100 | die "config/database.yml does not exist\n" | ||||
32 | unless -f $file; | ||||||
33 | |||||||
34 | 2 | 15 | my $all_config = LoadFile($file); | ||||
35 | 2 | 151 | my $mode = railsish_mode; | ||||
36 | 2 | 30 | return $all_config->{$mode}; | ||||
37 | } | ||||||
38 | |||||||
39 | sub _build_dsn { | ||||||
40 | 1 | 3 | my $self = shift; | ||||
41 | 1 | 14 | my $dsn = $ENV{RAILSISH_TEST_DSN} || $self->config->{dsn}; | ||||
42 | 1 | 8 | return $dsn; | ||||
43 | } | ||||||
44 | |||||||
45 | sub _build_kioku { | ||||||
46 | 1 | 3 | my $self = shift; | ||||
47 | 1 | 10 | my $config = $self->config; | ||||
48 | |||||||
49 | 1 | 8 | return KiokuDB->connect( | ||||
50 | $self->dsn, | ||||||
51 | create => 1, | ||||||
52 | user => $config->{user}, | ||||||
53 | password => $config->{password} | ||||||
54 | ); | ||||||
55 | } | ||||||
56 | |||||||
57 | sub search { | ||||||
58 | 0 | 0 | 0 | my ($self, %args) = @_; | |||
59 | 0 | 0 | my $kioku = $self->kioku; | ||||
60 | 0 | 0 | my $kioku_scope = $kioku->new_scope; | ||||
61 | |||||||
62 | 0 | 0 | if (ref($kioku->backend) eq "KiokuDB::Backend::Hash") { | ||||
63 | # With CLASS, it'll never find anything. | ||||||
64 | # Hash backend should only be used when testing, so this should be enough for now. | ||||||
65 | 0 | 0 | delete $args{CLASS}; | ||||
66 | } | ||||||
67 | 0 | 0 | return $kioku->search(\%args); | ||||
68 | } | ||||||
69 | |||||||
70 | sub lookup { | ||||||
71 | 0 | 0 | 0 | my ($self, @ids) = @_; | |||
72 | 0 | 0 | my $kioku = $self->kioku; | ||||
73 | 0 | 0 | my $kioku_scope = $kioku->new_scope; | ||||
74 | |||||||
75 | 0 | 0 | return $kioku->lookup(@ids); | ||||
76 | } | ||||||
77 | |||||||
78 | sub store { | ||||||
79 | 1 | 0 | 5 | my ($self, $obj) = @_; | |||
80 | 1 | 9 | my $kioku = $self->kioku; | ||||
81 | 1 | 12 | my $kioku_scope = $kioku->new_scope; | ||||
82 | |||||||
83 | 1 | 170 | $kioku->store($obj); | ||||
84 | } | ||||||
85 | |||||||
86 | sub object_to_id { | ||||||
87 | 0 | 0 | my ($self, $obj) = @_; | ||||
88 | 0 | my $kioku = $self->kioku; | |||||
89 | 0 | my $kioku_scope = $kioku->new_scope; | |||||
90 | 0 | return $kioku->object_to_id($obj); | |||||
91 | } | ||||||
92 | |||||||
93 | sub delete { | ||||||
94 | 0 | 0 | my ($self, $obj) = @_; | ||||
95 | 0 | my $kioku = $self->kioku; | |||||
96 | 0 | $kioku->delete($obj); | |||||
97 | } | ||||||
98 | |||||||
99 | __PACKAGE__->meta->make_immutable; | ||||||
100 |