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   386939 use base qw(Wikibase::Cache::Backend);
  5         48  
  5         2722  
4 5     5   46620 use strict;
  5         12  
  5         90  
5 5     5   26 use warnings;
  5         8  
  5         126  
6              
7 5     5   29 use Class::Utils qw(set_params);
  5         10  
  5         162  
8 5     5   2373 use Data::Handle;
  5         182297  
  5         254  
9 5     5   49 use Error::Pure qw(err);
  5         11  
  5         188  
10 5     5   2264 use Text::DSV;
  5         2748  
  5         1718  
11              
12             our $VERSION = 0.04;
13              
14             sub new {
15 4     4 1 3559 my ($class, @params) = @_;
16              
17             # Create object.
18 4         14 my $self = bless {}, $class;
19              
20             # Data handler.
21 4         40 $self->{'data_fh'} = Data::Handle->new(__PACKAGE__);
22              
23             # Process parameters.
24 4         2560 set_params($self, @params);
25              
26 4         55 $self->_load_data;
27              
28 4         119 return $self;
29             }
30              
31             sub _get {
32 4     4   1551 my ($self, $type, $key) = @_;
33              
34 4 100       13 if (exists $self->{static}->{$key}) {
35 3 50       8 if (exists $self->{static}->{$key}->{$type}) {
36 3         14 return $self->{static}->{$key}->{$type};
37             } else {
38 0         0 return;
39             }
40             } else {
41 1         5 return;
42             }
43             }
44              
45             sub _load_data {
46 4     4   10 my $self = shift;
47              
48             # Read data.
49 4         18 my $dsv = Text::DSV->new;
50 4         33 my $fh = $self->{'data_fh'};
51 4         47 while (my $data = <$fh>) {
52 167         10813 chomp $data;
53 167         399 my ($qid, $label, $description) = $dsv->parse_line($data);
54 167         3768 $self->{'static'}->{$qid}->{'label'} = $label;
55 167         567 $self->{'static'}->{$qid}->{'description'} = $description;
56             }
57              
58 4         201 return;
59             }
60              
61             sub _save {
62 1     1   69 my ($self, $type, $key, $value) = @_;
63              
64 1         5 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-2023 Michal Josef Špaček
178              
179             BSD 2-Clause License
180              
181             =head1 VERSION
182              
183             0.04
184              
185             =cut
186              
187             __DATA__