PerlConsole/Preferences.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 66 CC 7 SCAR 19.6
CC
7
Cov
0%
help
12.0
set
12.0
get
6.0
CRAP 7.1
1 package PerlConsole::Preferences;
2  
3 #
4 # This class hanldes all the preferences the user can change within the console.
5 #
6  
7 use strict;
8 use warnings;
9  
10 # The main data structure of the preferences,
11 # _valid_values contains list for each possible value.
12 sub init
13 {
14 my $self = {
15 _valid_values => {
16 output => ['scalar', 'dumper', 'yaml', 'dump', 'dds'],
17 },
18 _values => {
19 output => "scalar"
20 }
21 };
22 return $self;
23 }
24  
25 # the help messages, dynamically built with the data structure
26 sub help($;$)
27 {
28 my ($self, $pref) = @_;
29 if (!defined $pref) {
30 return "You can set a preference in the console with the following syntax:\n".
31 ":set <preference>=<value>\n".
32 "Available preferences are:\n\t- ".join("\n\t- ", keys(%{$self->{'_values'}}))."\n".
33 "see :help <preference> for details.";
34 }
35 else {
36 if (defined $self->{'_valid_values'}{$pref}) {
37 return "Valid values for preference \"$pref\" are: ".join(", ", @{$self->{'_valid_values'}{$pref}});
38 }
39 else {
40 return "No such preference: $pref";
41 }
42 }
43 }
44  
45 # create an empty preference object, ready for being set
46 sub new
47 {
48 my ($class) = @_;
49 my $self = PerlConsole::Preferences::init();
50 bless($self, $class);
51 return $self;
52 }
53  
54 # set a preference to a given value, making sure it's an available value, and
55 # that the value given is valid.
56 sub set($$$)
57 {
58 my ($self, $pref, $val) = @_;
59 unless (defined $self->{'_valid_values'}{$pref}) {
60 return 0;
61 }
62 unless (grep /$val/, @{$self->{'_valid_values'}{$pref}}) {
63 return 0;
64 }
65 $self->{'_values'}{$pref} = $val;
66 }
67  
68 # retrurn the preference's value
69 sub get($$)
70 {
71 my ($self, $pref) = @_;
72 unless (exists $self->{'_values'}{$pref}) {
73 return 0;
74 }
75 return $self->{'_values'}{$pref};
76 }
77  
78 # returns a list of all available preferences
79 sub getPreferences($)
80 {
81 my ($self) = @_;
82 return keys %{$self->{"_valid_values"}};
83 }
84  
85 # returns a list of all possible values of a preference
86 sub getValidValues($$)
87 {
88 my ($self, $pref) = @_;
89 return [] unless defined $self->{'_valid_values'}{$pref};
90 return $self->{'_valid_values'}{$pref};
91 }
92  
93  
94 # END
95 1;