File Coverage

blib/lib/Wikibase/Cache/Backend/Basic.pm
Criterion Covered Total %
statement 43 44 97.7
branch 3 4 75.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 58 60 96.6


line stmt bran cond sub pod time code
1             package Wikibase::Cache::Backend::Basic;
2              
3 5     5   672007 use base qw(Wikibase::Cache::Backend);
  5         10  
  5         3072  
4 5     5   65185 use strict;
  5         12  
  5         132  
5 5     5   23 use warnings;
  5         11  
  5         271  
6              
7 5     5   26 use Class::Utils qw(set_params);
  5         14  
  5         266  
8 5     5   5052 use Data::Handle;
  5         215270  
  5         327  
9 5     5   43 use Error::Pure qw(err);
  5         9  
  5         232  
10 5     5   2471 use Text::DSV;
  5         3602  
  5         2214  
11              
12             our $VERSION = 0.05;
13              
14             sub new {
15 4     4 1 643718 my ($class, @params) = @_;
16              
17             # Create object.
18 4         15 my $self = bless {}, $class;
19              
20             # Data handler.
21 4         53 $self->{'data_fh'} = Data::Handle->new(__PACKAGE__);
22              
23             # Process parameters.
24 4         2847 set_params($self, @params);
25              
26 4         49 $self->_load_data;
27              
28 4         71 return $self;
29             }
30              
31             sub _get {
32 4     4   2434 my ($self, $type, $key) = @_;
33              
34 4 100       16 if (exists $self->{static}->{$key}) {
35 3 50       10 if (exists $self->{static}->{$key}->{$type}) {
36 3         13 return $self->{static}->{$key}->{$type};
37             } else {
38 0         0 return;
39             }
40             } else {
41 1         10 return;
42             }
43             }
44              
45             sub _load_data {
46 4     4   9 my $self = shift;
47              
48             # Read data.
49 4         36 my $dsv = Text::DSV->new;
50 4         37 my $fh = $self->{'data_fh'};
51 4         67 while (my $data = <$fh>) {
52 173         10570 chomp $data;
53 173         451 my ($qid, $label, $description) = $dsv->parse_line($data);
54 173         3728 $self->{'static'}->{$qid}->{'label'} = $label;
55 173         655 $self->{'static'}->{$qid}->{'description'} = $description;
56             }
57              
58 4         188 return;
59             }
60              
61             sub _save {
62 1     1   103 my ($self, $type, $key, $value) = @_;
63              
64 1         6 err __PACKAGE__." doesn't implement save() method.";
65             }
66              
67             1;
68              
69             =pod
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             Wikibase::Cache::Backend::Basic - Wikibase cache backend to local static basic ids (units, common properties)
76              
77             =head1 SYNOPSIS
78              
79             use Wikibase::Cache::Backend::Basic;
80              
81             my $obj = Wikibase::Cache::Backend::Basic->new;
82             my $value = $obj->get($type, $key);
83             $obj->save($type, $key, $value);
84              
85             =head1 METHODS
86              
87             =head2 C
88              
89             my $obj = Wikibase::Cache::Backend::Basic->new;
90              
91             Constructor.
92              
93             =over 8
94              
95             =item * C
96              
97             Data file handler from which is mapping fetched.
98             Data file is in format parsed by L.
99              
100             Default value is mapping in this file on the end.
101              
102             =back
103              
104             Returns instance of object.
105              
106             =head2 C
107              
108             my $value = $obj->get($type, $key);
109              
110             Get cache value for C<$type> and C<$key>.
111             Possible types are 'description' and 'label'.
112              
113             Returns string.
114              
115             =head2 C
116              
117             $obj->save($type, $key, $value);
118              
119             Save method is not implemented in this implementation of backend.
120             Goes to error.
121              
122             =head1 ERROR
123              
124             new():
125             From Class::Utils::set_params():
126             Unknown parameter '%s'.
127              
128             get():
129             Type '%s' isn't supported.
130             Type must be defined.';
131              
132             save():
133             Type '%s' isn't supported.
134             Type must be defined.';
135             Wikibase::Cache::Backend::Basic doesn't implement save() method.
136            
137              
138             =head1 EXAMPLE
139              
140             =for comment filename=p31_label_and_description.pl
141              
142             use strict;
143             use warnings;
144              
145             use Wikibase::Cache::Backend::Basic;
146              
147             my $obj = Wikibase::Cache::Backend::Basic->new;
148              
149             # Print out.
150             print 'P31 label: '.$obj->get('label', 'P31')."\n";
151             print 'P31 description: '.$obj->get('description', 'P31')."\n";
152              
153             # Output:
154             # P31 label: instance of
155             # P31 description: that class of which this subject is a particular example and member
156              
157             =head1 DEPENDENCIES
158              
159             L,
160             L,
161             L,
162             L,
163             L.
164              
165             =head1 REPOSITORY
166              
167             L
168              
169             =head1 AUTHOR
170              
171             Michal Josef Špaček L
172              
173             L
174              
175             =head1 LICENSE AND COPYRIGHT
176              
177             © 2021-2025 Michal Josef Špaček
178              
179             BSD 2-Clause License
180              
181             =head1 VERSION
182              
183             0.05
184              
185             =cut
186              
187             __DATA__