1 |
package Webpacus::Model::Databases; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use base 'Catalyst::Model'; |
6 |
use Text::Iconv; |
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 |
my $from = $c->config->{config_encoding}; |
34 |
my $to = $c->config->{catalyst_encoding}; |
35 |
|
36 |
$self->{iconv} = new Text::Iconv($from, $to) if ($from && $to); |
37 |
|
38 |
return $self; |
39 |
} |
40 |
|
41 |
=head1 convert |
42 |
|
43 |
Convert encodings from C<config_encoding> to C<catalyst_encoding) |
44 |
|
45 |
my $utf8 = $self->convert('foobar'); |
46 |
|
47 |
=cut |
48 |
|
49 |
sub convert { |
50 |
my $self = shift; |
51 |
my $val = shift || return; |
52 |
return $val unless ($self->{iconv}); |
53 |
|
54 |
return $self->{iconv}->convert($val); |
55 |
} |
56 |
|
57 |
=head1 list_inputs |
58 |
|
59 |
Returns list of databases with C<input>, C<name>, C<prefix> defined in |
60 |
Webpacus configuration file C<config.yml> under C<databases> |
61 |
|
62 |
my @dbs = $c->comp('Model::Databases')->list_inputs; |
63 |
|
64 |
=cut |
65 |
|
66 |
sub list_inputs { |
67 |
my $self = shift; |
68 |
|
69 |
my @databases; |
70 |
|
71 |
# convert to resonable format for TT |
72 |
foreach my $db (keys %{ $self->{databases} }) { |
73 |
my $d = $self->{databases}->{$db} || die; |
74 |
my $inputs = $d->{input} || next; |
75 |
$inputs = [ $inputs ] if (ref($inputs) ne 'ARRAY'); |
76 |
|
77 |
foreach my $i ( @{ $inputs } ) { |
78 |
my $el = { |
79 |
input => $self->convert( $i->{name} ), |
80 |
name => $self->convert( $d->{name} || $d ), |
81 |
prefix => $self->convert( $db . '/' . $i->{name} ), |
82 |
}; |
83 |
push @databases, $el; |
84 |
} |
85 |
} |
86 |
|
87 |
return @databases; |
88 |
} |
89 |
|
90 |
=head1 list |
91 |
|
92 |
Returns just of just unique databases with C<name> and C<prefix>. |
93 |
|
94 |
=cut |
95 |
|
96 |
sub list { |
97 |
my $self = shift; |
98 |
|
99 |
my @databases; |
100 |
|
101 |
foreach my $db (keys %{ $self->{databases} }) { |
102 |
my $d = $self->{databases}->{$db} || die; |
103 |
|
104 |
my $el = { |
105 |
name => $self->convert( $d->{name} || $db ), |
106 |
prefix => $self->convert( $db ), |
107 |
}; |
108 |
push @databases, $el; |
109 |
} |
110 |
|
111 |
return @databases; |
112 |
} |
113 |
|
114 |
=head1 AUTHOR |
115 |
|
116 |
Dobrica Pavlinusic, C< <<dpavlin@rot13.org>> > |
117 |
|
118 |
=head1 LICENSE |
119 |
|
120 |
This library is free software, you can redistribute it and/or modify |
121 |
it under the same terms as Perl itself. |
122 |
|
123 |
=cut |
124 |
|
125 |
1; |