| 1 | package User::Config::DB; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.01_01'; | |||||||||
| 8 | 1 | $VERSION = eval $VERSION; # see L<perlmodstyle> | |||||||||
| 9 | |||||||||||
| 10 | 1 | requires 'get'; | |||||||||
| 11 | 1 | requires 'set'; | |||||||||
| 12 | |||||||||||
| 13 | =pod | ||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | User::Config::DB - This defines the role, the database-backends are playing. | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | To store the value, a user set for a given option, L<User::Config> uses a | ||||||||||
| 22 | database-backend. These backends must live within the namespace | ||||||||||
| 23 | User::Config::DB:: and consume this role. | ||||||||||
| 24 | |||||||||||
| 25 | =head2 CONSUMER-METHODS | ||||||||||
| 26 | |||||||||||
| 27 | The interface was held as simple as possible. To consume this role, the | ||||||||||
| 28 | following methods have to be implemented. | ||||||||||
| 29 | |||||||||||
| 30 | =head3 C<<$db->set($package, $user, $option_name, $context, $value)>> | ||||||||||
| 31 | |||||||||||
| 32 | The C<set> method has to be implemented. Whenever a user sets a speicific | ||||||||||
| 33 | option to a new value, this method is called and has to take care of storing | ||||||||||
| 34 | this piece of information. | ||||||||||
| 35 | |||||||||||
| 36 | The parameters are as following: | ||||||||||
| 37 | |||||||||||
| 38 | =over 4 | ||||||||||
| 39 | |||||||||||
| 40 | =item C<$package> | ||||||||||
| 41 | |||||||||||
| 42 | Contains the package or namespace for the module in question. This usally is | ||||||||||
| 43 | the package-name of the module, declaring the option. | ||||||||||
| 44 | |||||||||||
| 45 | =item C<$user> | ||||||||||
| 46 | |||||||||||
| 47 | The name of the current contexts user. | ||||||||||
| 48 | |||||||||||
| 49 | =item C<$option_name> | ||||||||||
| 50 | |||||||||||
| 51 | The name of the option to set ( within the package ). | ||||||||||
| 52 | |||||||||||
| 53 | =item C<$context> | ||||||||||
| 54 | |||||||||||
| 55 | The current context. | ||||||||||
| 56 | |||||||||||
| 57 | =item C<$value> | ||||||||||
| 58 | |||||||||||
| 59 | The new value to set. | ||||||||||
| 60 | |||||||||||
| 61 | =back | ||||||||||
| 62 | |||||||||||
| 63 | =head3 C<<$db->get($package, $user, $option_name, $context)>> | ||||||||||
| 64 | |||||||||||
| 65 | The C<get> method has to be implemented by the corresponding backend. It | ||||||||||
| 66 | returns the previous set value or undef, if no value was set. | ||||||||||
| 67 | |||||||||||
| 68 | The parameters work are the same as for C<set>. | ||||||||||
| 69 | |||||||||||
| 70 | =head3 C<<$db->isset($package, $user, $option_name, $context)>> | ||||||||||
| 71 | |||||||||||
| 72 | Optionally, the backend can implement the C<isset>-method. This will be called | ||||||||||
| 73 | before C<get> is called. If the user has set this option in advance, the method | ||||||||||
| 74 | should return true. If not, false should be returned. Then C<get> isn't called at | ||||||||||
| 75 | all and the default value will be submitted to the caller. | ||||||||||
| 76 | |||||||||||
| 77 | The parameters work are the same as for C<set>. | ||||||||||
| 78 | |||||||||||
| 79 | If this isn't implemented C<get> is always called. The default value will then | ||||||||||
| 80 | be returned, if C<get> returns undef. | ||||||||||
| 81 | |||||||||||
| 82 | =head2 INTERNALS | ||||||||||
| 83 | |||||||||||
| 84 | The following information aren't needed to write new code. There here for | ||||||||||
| 85 | completness only. | ||||||||||
| 86 | |||||||||||
| 87 | While C<set> is just checking, wether the user is valid, C<get> is completly | ||||||||||
| 88 | wrapped in this role. The wrapper checks wether the backend implements C<isset> | ||||||||||
| 89 | and returns the user-set value or the default, if the setting isn't stored. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | around set => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 94 | 0 | my $code = shift; | |||||||||
| 95 | 0 | my ($self, $namespace, $user, $name, $ctx, $value) = @_; | |||||||||
| 96 | 0 | my $opts = User::Config::instance()->options()->{$namespace}->{$name}; | |||||||||
| 97 | 0 | ▶ | return if $opts->{noset}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | ▶ | return unless $user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | return &$code(@_); | |||||||||
| 100 | 1 | }; | |||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | around get => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 103 | 0 | my $code = shift; | |||||||||
| 104 | 0 | my $self = shift; | |||||||||
| 105 | 0 | my ($namespace, $user, $name, $ctx) = @_; | |||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | return $self->default(@_) unless $user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | ▶ | if($self->can("isset")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | ▶ | return &$code($self, @_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | if $self->isset(@_); | ||||||||||
| 111 | 0 | return $self->default(@_); | |||||||||
| 112 | } | ||||||||||
| 113 | 0 | my $ret = &$code( $self, @_); | |||||||||
| 114 | 0 | ▶ | return $ret if defined $ret; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | return $self->default(@_); | |||||||||
| 116 | 1 | }; | |||||||||
| 117 | |||||||||||
| 118 | =pod | ||||||||||
| 119 | |||||||||||
| 120 | The default value is given while the option is declared. If the default value | ||||||||||
| 121 | is a code-reference, the code is called. The returned value is immediatly | ||||||||||
| 122 | saved in the backend, so that it get's returned the next time the option is | ||||||||||
| 123 | retrieved. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub default { | ||||||||
|
Subroutine
default: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 128 | 0 | my ($self, $namespace, $user, $name, $ctx) = @_; | |||||||||
| 129 | 0 | my $def; | |||||||||
| 130 | 0 | my $opts = User::Config::instance()->options()->{$namespace}->{$name}; | |||||||||
| 131 | 0 | ▶ | $def = $opts->{anon_default} unless $user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | ▶ | $def = $opts->{default} unless $def; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | ▶ | return unless $def; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | return $def unless ref $def; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | if(ref $def eq "CODE") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | my $ret = &$def($ctx, $name); | |||||||||
| 137 | 0 | $self->set($namespace, $user, $name, $ctx, $ret); | |||||||||
| 138 | 0 | return $ret; | |||||||||
| 139 | } | ||||||||||
| 140 | 0 | return $def; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | =head1 SEE ALSO | ||||||||||
| 145 | |||||||||||
| 146 | L<User::Config> | ||||||||||
| 147 | L<User::Config::DB::DBIC> | ||||||||||
| 148 | L<User::Config::DB::Ldap> | ||||||||||
| 149 | L<User::Config::DB::Keyed> | ||||||||||
| 150 | |||||||||||
| 151 | Further backends are welcome. | ||||||||||
| 152 | |||||||||||
| 153 | =head1 AUTHOR | ||||||||||
| 154 | |||||||||||
| 155 | Benjamin Tietz E<lt>benjamin@micronet24.deE<gt> | ||||||||||
| 156 | |||||||||||
| 157 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 158 | |||||||||||
| 159 | Copyright (C) 2011 by Benjamin Tietz | ||||||||||
| 160 | |||||||||||
| 161 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 162 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||
| 163 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 164 | |||||||||||
| 165 | =cut | ||||||||||
| 166 | |||||||||||
| 167 | 1 | 1; | |||||||||
| 168 | |||||||||||