1 |
package Webpacus::Model::Databases; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use base 'Catalyst::Model'; |
6 |
use Encode qw/decode/; |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
Webpacus::Model::Databases - Catalyst Model to represent available databases |
11 |
|
12 |
=head1 SYNOPSIS |
13 |
|
14 |
See L<Webpacus> |
15 |
|
16 |
=head1 DESCRIPTION |
17 |
|
18 |
Get databases from config |
19 |
|
20 |
=cut |
21 |
|
22 |
sub new { |
23 |
my ( $self, $c, $config ) = @_; |
24 |
|
25 |
$self = $self->NEXT::new($c, $config); |
26 |
$self->config($config); |
27 |
|
28 |
# get databases from config |
29 |
$self->{databases} = $c->config->{databases} or |
30 |
$c->log->fail("didn't find databases in config"); |
31 |
$self->{log} = $c->log; |
32 |
|
33 |
$c->log->debug("using config encoding ", $self->config->{config_encoding}); |
34 |
|
35 |
return $self; |
36 |
} |
37 |
|
38 |
=head1 convert |
39 |
|
40 |
Convert encodings from C<config_encoding> |
41 |
|
42 |
my $utf8 = $self->convert('foobar'); |
43 |
|
44 |
=cut |
45 |
|
46 |
sub convert { |
47 |
my $self = shift; |
48 |
my $val = shift || return; |
49 |
my $encoding = $self->config->{config_encoding} || return $val; |
50 |
return decode($encoding, $val); |
51 |
} |
52 |
|
53 |
=head1 list_inputs |
54 |
|
55 |
Returns list of databases with C<input>, C<name>, C<prefix> defined in |
56 |
Webpacus configuration file C<config.yml> under C<databases> |
57 |
|
58 |
my @dbs = $c->comp('Model::Databases')->list_inputs; |
59 |
|
60 |
=cut |
61 |
|
62 |
sub list_inputs { |
63 |
my $self = shift; |
64 |
|
65 |
my @databases; |
66 |
|
67 |
# convert to resonable format for TT |
68 |
foreach my $db (keys %{ $self->{databases} }) { |
69 |
my $d = $self->{databases}->{$db} || die; |
70 |
my $inputs = $d->{input} || next; |
71 |
$inputs = [ $inputs ] if (ref($inputs) ne 'ARRAY'); |
72 |
|
73 |
foreach my $i ( @{ $inputs } ) { |
74 |
my $el = { |
75 |
input => $self->convert( $i->{name} ), |
76 |
name => $self->convert( $d->{name} || $d ), |
77 |
prefix => $self->convert( $db . '/' . $i->{name} ), |
78 |
}; |
79 |
push @databases, $el; |
80 |
} |
81 |
} |
82 |
|
83 |
return @databases; |
84 |
} |
85 |
|
86 |
=head1 list |
87 |
|
88 |
Returns just of just unique databases with C<name> and C<prefix>. |
89 |
|
90 |
my @dbs = $c->comp('Model::Databases')->list; |
91 |
|
92 |
You can also return just databases that have C<input> defined (that are not |
93 |
empty databases used for agregation) if you add option C<require_input>. |
94 |
|
95 |
my @db_2 = $c->comp('Model::Databases')->list( require_input => 1 ); |
96 |
|
97 |
=cut |
98 |
|
99 |
sub list { |
100 |
my $self = shift; |
101 |
|
102 |
my $args = shift; |
103 |
|
104 |
my @databases; |
105 |
|
106 |
foreach my $db (keys %{ $self->{databases} }) { |
107 |
my $d = $self->{databases}->{$db} || die; |
108 |
|
109 |
if ($args->{require_input}) { |
110 |
next unless ($d->{input}); |
111 |
} |
112 |
|
113 |
my $el = { |
114 |
name => $self->convert( $d->{name} || $db ), |
115 |
prefix => $self->convert( $db ), |
116 |
}; |
117 |
push @databases, $el; |
118 |
} |
119 |
|
120 |
return @databases; |
121 |
} |
122 |
|
123 |
=head1 AUTHOR |
124 |
|
125 |
Dobrica Pavlinusic, C< <<dpavlin@rot13.org>> > |
126 |
|
127 |
=head1 LICENSE |
128 |
|
129 |
This library is free software, you can redistribute it and/or modify |
130 |
it under the same terms as Perl itself. |
131 |
|
132 |
=cut |
133 |
|
134 |
1; |