| 1 | package OpusVL::SysParams; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use Data::Munge qw/elem/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | ▶ | require Config::JFDI; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | require OpusVL::SysParams::Schema; | |||||||||
| 17 | 1 | my $config = Config::JFDI->new(name => __PACKAGE__); | |||||||||
| 18 | 1 | my $config_hash = $config->get; | |||||||||
| 19 | 1 | my $schema = OpusVL::SysParams::Schema->connect( @{$config_hash->{'Model::SysParams'}->{connect_info}} ); | |||||||||
| 20 | 1 | return $schema; | |||||||||
| 21 | } | ||||||||||
| 22 | 4 | ); | |||||||||
| 23 | |||||||||||
| 24 | # ABSTRACT: Module to handle system wide parameters | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 4 | our $VERSION = '0.17'; | |||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | sub get { | ||||||||||
| 32 | 10 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 10 | my $schema = $self->schema; | |||||||||
| 34 | 10 | return $schema->resultset('SysInfo')->get(@_); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | sub get_or_set { | ||||||||||
| 39 | 2 | ▶ | my ($self, $name, $default_sub) = @_; | ||||||||
|
Subroutine
get_or_set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 2 | ▶ | if (elem $name, [$self->key_names]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | return $self->get($name); | |||||||||
| 42 | } | ||||||||||
| 43 | else { | ||||||||||
| 44 | 1 | my $value = $default_sub->($self); | |||||||||
| 45 | 1 | $self->set($name, $value); | |||||||||
| 46 | 1 | return $value; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | sub del { | ||||||||||
| 52 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 53 | 1 | my $schema = $self->schema; | |||||||||
| 54 | 1 | return $schema->resultset('SysInfo')->del(@_); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | sub key_names { | ||||||||||
| 59 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
key_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | 5 | my $schema = $self->schema; | |||||||||
| 61 | 5 | return $schema->resultset('SysInfo')->key_names(@_); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | sub set { | ||||||||||
| 66 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 6 | my $schema = $self->schema; | |||||||||
| 68 | 6 | return $schema->resultset('SysInfo')->set(@_); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | sub set_json { | ||||||||||
| 73 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 74 | 2 | my $name = shift; | |||||||||
| 75 | 2 | my $val = shift; | |||||||||
| 76 | 2 | my $schema = $self->schema; | |||||||||
| 77 | 2 | my $obj = JSON->new->allow_nonref->decode($val); | |||||||||
| 78 | 2 | return $schema->resultset('SysInfo')->set($name, $obj); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | sub get_json { | ||||||||||
| 83 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_json: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 84 | 2 | my $schema = $self->schema; | |||||||||
| 85 | |||||||||||
| 86 | 2 | my $val = $schema->resultset('SysInfo')->get(@_); | |||||||||
| 87 | 2 | ▶ | return if !$val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | return JSON->new->allow_nonref->encode($val); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 4 | 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.17 | ||||||||||
| 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 | Example: | ||||||||||
| 157 | |||||||||||
| 158 | $params->get_or_set('partner.titles', sub { 'Mr|Mrs|Miss|Mx' }); | ||||||||||
| 159 | |||||||||||
| 160 | =head2 del | ||||||||||
| 161 | |||||||||||
| 162 | Delete a system parameter. The key name is simply a string. | ||||||||||
| 163 | |||||||||||
| 164 | =head2 key_names | ||||||||||
| 165 | |||||||||||
| 166 | Returns the keys of the system parameters. | ||||||||||
| 167 | |||||||||||
| 168 | =head2 set | ||||||||||
| 169 | |||||||||||
| 170 | Set a system parameter. The key name is simply a string. It's suggested you use some | ||||||||||
| 171 | kind of schema like 'system.key' to prevent name clashes with other unoriginal programmers. | ||||||||||
| 172 | |||||||||||
| 173 | The value can be any data structure so long as it doesn't contain code. | ||||||||||
| 174 | |||||||||||
| 175 | =head2 set_json | ||||||||||
| 176 | |||||||||||
| 177 | Set a system parameter. This allows you to pass the object encoded as JSON in order to make it simpler | ||||||||||
| 178 | for web interfaces to talk to the settings. | ||||||||||
| 179 | |||||||||||
| 180 | =head2 get_json | ||||||||||
| 181 | |||||||||||
| 182 | Returns the value encoded in json. Primarily for talking with web interfaces. | ||||||||||
| 183 | |||||||||||
| 184 | =head1 SUPPORT | ||||||||||
| 185 | |||||||||||
| 186 | If you require assistance, support, or further development of this software, please contact OpusVL using the details below: | ||||||||||
| 187 | |||||||||||
| 188 | =over 4 | ||||||||||
| 189 | |||||||||||
| 190 | =item * | ||||||||||
| 191 | |||||||||||
| 192 | Telephone: +44 (0)1788 298 410 | ||||||||||
| 193 | |||||||||||
| 194 | =item * | ||||||||||
| 195 | |||||||||||
| 196 | Email: community@opusvl.com | ||||||||||
| 197 | |||||||||||
| 198 | =item * | ||||||||||
| 199 | |||||||||||
| 200 | Web: L<http://opusvl.com> | ||||||||||
| 201 | |||||||||||
| 202 | =back | ||||||||||
| 203 | |||||||||||
| 204 | =head1 AUTHOR | ||||||||||
| 205 | |||||||||||
| 206 | OpusVL - www.opusvl.com | ||||||||||
| 207 | |||||||||||
| 208 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 209 | |||||||||||
| 210 | This software is copyright (c) 2011 - 2016 by OpusVL - www.opusvl.com. | ||||||||||
| 211 | |||||||||||
| 212 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 213 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 214 | |||||||||||
| 215 | =cut | ||||||||||