line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OpusVL::SysParams; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
88387
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
5
|
1
|
|
|
1
|
|
175
|
use JSON; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Data::Munge qw/elem/; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Moose; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has 'schema' => (isa => 'DBIx::Class::Schema', is => 'ro', required => 1, |
11
|
|
|
|
|
|
|
default => sub |
12
|
|
|
|
|
|
|
{ |
13
|
|
|
|
|
|
|
# this means we only load Config::JFDI and create our schema if they |
14
|
|
|
|
|
|
|
# don't specify their own schema. |
15
|
|
|
|
|
|
|
require Config::JFDI; |
16
|
|
|
|
|
|
|
require OpusVL::SysParams::Schema; |
17
|
|
|
|
|
|
|
my $config = Config::JFDI->new(name => __PACKAGE__); |
18
|
|
|
|
|
|
|
my $config_hash = $config->get; |
19
|
|
|
|
|
|
|
my $schema = OpusVL::SysParams::Schema->connect( @{$config_hash->{'Model::SysParams'}->{connect_info}} ); |
20
|
|
|
|
|
|
|
return $schema; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# ABSTRACT: Module to handle system wide parameters |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
our $VERSION = '0.20'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub get { |
32
|
|
|
|
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
my $schema = $self->schema; |
34
|
|
|
|
|
|
|
return $schema->resultset('SysInfo')->get(@_); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub get_or_set { |
39
|
|
|
|
|
|
|
my ($self, $name, $default_sub, $type) = @_; |
40
|
|
|
|
|
|
|
if (elem $name, [$self->key_names]) { |
41
|
|
|
|
|
|
|
return $self->get($name); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
|
|
|
|
|
|
my $value = $default_sub->($self); |
45
|
|
|
|
|
|
|
$self->set($name, $value, $type); |
46
|
|
|
|
|
|
|
return $value; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub del { |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
my $schema = $self->schema; |
54
|
|
|
|
|
|
|
return $schema->resultset('SysInfo')->del(@_); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub key_names { |
59
|
|
|
|
|
|
|
my $self = shift; |
60
|
|
|
|
|
|
|
my $schema = $self->schema; |
61
|
|
|
|
|
|
|
return $schema->resultset('SysInfo')->key_names(@_); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub set { |
66
|
|
|
|
|
|
|
my $self = shift; |
67
|
|
|
|
|
|
|
my $schema = $self->schema; |
68
|
|
|
|
|
|
|
return $schema->resultset('SysInfo')->set(@_); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub set_json { |
73
|
|
|
|
|
|
|
my $self = shift; |
74
|
|
|
|
|
|
|
my $name = shift; |
75
|
|
|
|
|
|
|
my $val = shift; |
76
|
|
|
|
|
|
|
my $schema = $self->schema; |
77
|
|
|
|
|
|
|
my $obj = JSON->new->allow_nonref->decode($val); |
78
|
|
|
|
|
|
|
return $schema->resultset('SysInfo')->set($name, $obj); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_json { |
83
|
|
|
|
|
|
|
my $self = shift; |
84
|
|
|
|
|
|
|
my $schema = $self->schema; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $val = $schema->resultset('SysInfo')->get(@_); |
87
|
|
|
|
|
|
|
return if !$val; |
88
|
|
|
|
|
|
|
return JSON->new->allow_nonref->encode($val); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; # End of OpusVL::SysParams |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=pod |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=encoding UTF-8 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 NAME |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
OpusVL::SysParams - Module to handle system wide parameters |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 VERSION |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
version 0.20 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
This module handles system wide parameters. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use OpusVL::SysParams; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $sys_param = OpusVL::SysParams->new(); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# or |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $sys_param = OpusVL::SysParams->new({ schema => $schema}); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $val = $sys_param->get('login.failures'); |
121
|
|
|
|
|
|
|
$sys_param->set('login.failures', 3); |
122
|
|
|
|
|
|
|
... |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 METHODS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 new |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
If the constructor is called without a schema specified it will attempt to load up a schema based |
129
|
|
|
|
|
|
|
on a config file in the catalyst style for the name 'OpusVL::SysParams'. This config file should |
130
|
|
|
|
|
|
|
have a Model::SysParams section containing the config. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
<Model::SysParams> |
133
|
|
|
|
|
|
|
connect_info dbi:Pg:dbname=test1 |
134
|
|
|
|
|
|
|
connect_info user |
135
|
|
|
|
|
|
|
connect_info password |
136
|
|
|
|
|
|
|
</Model::SysParams> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Note that you must specify at least 2 connect_info parameters even if you are using SQLite otherwise |
139
|
|
|
|
|
|
|
the code will crash. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 get |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Get a system parameter. The key name is simply a string. It's suggested you use some |
144
|
|
|
|
|
|
|
kind of schema like 'system.key' to prevent name clashes with other unoriginal programmers. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 get_or_set |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Get a system parameter, setting it to a default if it doesn't already exist. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$params->get_or_set($name, $default_sub); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
C<$name> - the name of the system parameter |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
C<$default_sub> - A CODEREF returning the default value. C<$params> (your instance of L<OpusVL::SysParams>) is passed as the first argument. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
C<$type> - We will try to determine the type from your C<$default_sub>, but if it's unclear, you may wish to be explicit here. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Example: |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$params->get_or_set('partner.titles', sub { 'Mr|Mrs|Miss|Mx' }); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 del |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Delete a system parameter. The key name is simply a string. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 key_names |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Returns the keys of the system parameters. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 set |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Set a system parameter. The key name is simply a string. It's suggested you use some |
173
|
|
|
|
|
|
|
kind of schema like 'system.key' to prevent name clashes with other unoriginal programmers. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
The value can be any data structure so long as it doesn't contain code. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 set_json |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Set a system parameter. This allows you to pass the object encoded as JSON in order to make it simpler |
180
|
|
|
|
|
|
|
for web interfaces to talk to the settings. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 get_json |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Returns the value encoded in json. Primarily for talking with web interfaces. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SUPPORT |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
If you require assistance, support, or further development of this software, please contact OpusVL using the details below: |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over 4 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item * |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Telephone: +44 (0)1788 298 410 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item * |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Email: community@opusvl.com |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item * |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Web: L<http://opusvl.com> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=back |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
OpusVL - www.opusvl.com |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
This software is copyright (c) 2011 - 2016 by OpusVL - www.opusvl.com. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
215
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |