File: Driver.pm

package info (click to toggle)
libmixin-extrafields-perl 0.140003-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 411; makefile: 2
file content (323 lines) | stat: -rw-r--r-- 9,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

use strict;
use warnings;

package Mixin::ExtraFields::Driver 0.140003;
# ABSTRACT: a backend for extra field storage

use Carp ();
use Sub::Install ();

#pod =head1 SYNOPSIS
#pod
#pod This is really not something you'd use on your own, it's just used by
#pod Mixin::ExtraFields, but if you insist...
#pod
#pod   my $driver = Mixin::ExtraFields::Driver::Phlogiston->from_args(\%arg);
#pod
#pod   $driver->set($obj, $obj_id, flammable => "very!");
#pod
#pod =head1 DESCRIPTION
#pod
#pod Mixin::ExtraFields::Driver is a base class for drivers used by
#pod Mixin::ExtraFields -- hence the name.  A driver is expected to store and
#pod retrieve data keyed to an object and a name or key.  It can store this in any
#pod way it likes, and does not need to guarantee persistence across processes.
#pod
#pod =head1 SUBCLASSING
#pod
#pod All drivers must implement the four methods listed below.  The base class has
#pod implementations of these methods which will die noisily (C<confess>-ing) when
#pod called.
#pod
#pod Almost all methods are passed the same data as their first two arguments:
#pod C<$object>, the object for which the driver is to find or alter data, and
#pod C<$id>, that object's unique id.  While this may be slighly redundant, it keeps
#pod the id-finding call in one place.
#pod
#pod =head2 from_args
#pod
#pod   my $driver = Mixin::ExtraFields::Driver::Subclass->from_args(\%arg);
#pod
#pod This method must return a driver object appropriate to the given args.  It is
#pod not called C<new> because it need not return a new object for each call to it.
#pod Returning identical objects for identical configurations may be safe for some
#pod driver implementations, and it is expressly allowed.
#pod
#pod The arguments passed to this method are those given as the C<driver> option to
#pod the C<fields> import group in Mixin::ExtraFields, less the C<class> option.
#pod
#pod =head2 get_all_detailed_extra
#pod
#pod   my %extra = $driver->get_all_detailed_extra($object, $id);
#pod
#pod This method must return all available information about all existing extra
#pod fields for the given object.  It must be returned as a list of name/value
#pod pairs.  The values must be references to hashes.  Each hash must have an entry
#pod for the key C<value> giving the value for that name.
#pod
#pod =head2 set_extra
#pod
#pod   $driver->set_extra($object, $id, $name, $value);
#pod
#pod This method must set the named extra to the given value.
#pod
#pod =head2 delete_extra
#pod
#pod   $driver->delete_extra($object, $id, $name);
#pod
#pod This method must delete the named extra, causing it to cease to exist.
#pod
#pod =cut

BEGIN {
  for my $name (qw(from_args get_all_detailed_extra set_extra delete_extra)) {
    Sub::Install::install_sub({
      as   => $name,
      code => sub { Carp::confess "method $name called but not implemented!" },
    });
  }
}

#pod =head1 OPTIMIZING
#pod
#pod The methods below can all be implemented in terms of those above.  If they are
#pod not provided by the subclass, basic implementations exist.  These
#pod implementations may be less efficient than implementations crafted for the
#pod specifics of the storage engine behind the driver, so authors of driver
#pod subclasses should consider implementing these methods.
#pod
#pod =head2 get_all_extra
#pod
#pod   my %extra = $driver->get_all_extra($object, $id);
#pod
#pod This method behaves like C<get_all_detailed_extra>, above, but provides the
#pod entry's value, not a detailed hashref, as the value for each entry.
#pod
#pod =cut

sub get_all_extra {
  my ($self, $object, $id) = @_;
  
  my %extra  = $self->get_all_detailed_extra($object, $id);
  my @simple = map { $_ => $extra{$_}{value} } keys %extra;
}

#pod =head2 get_extra
#pod
#pod =head2 get_detailed_extra
#pod
#pod   my $value = $driver->get_extra($object, $id, $name);
#pod
#pod   my $hash = $driver->get_detailed_extra($object, $id, $name);
#pod
#pod These methods return a single value requested by name, either as the value
#pod itself or a detailed hashref describing it.
#pod
#pod =cut

sub get_extra {
  my ($self, $object, $id, $name) = @_;
  
  my $extra = $self->get_detailed_extra($object, $id, $name);
  return $extra ? $extra->{value} : ();
}

sub get_detailed_extra {
  my ($self, $object, $id, $name) = @_;

  my %extra = $self->get_all_detailed_extra($object, $id);
  return exists $extra{$name} ? $extra{$name} : ();
}

#pod =head2 get_all_extra_names
#pod
#pod   my @names = $driver->get_all_extra_names($object, $id);
#pod
#pod This method returns the names of all existing extras for the given object.
#pod
#pod =cut

sub get_all_extra_names {
  my ($self, $object, $id) = @_;
  my %extra = $self->get_all_detailed_extra($object, $id);
  return keys %extra;
}

#pod =head2 exists_extra
#pod
#pod   if ($driver->exists_extra($object, $id, $name)) { ... }
#pod
#pod This method returns true if an entry exists for the given name and false
#pod otherwise.
#pod
#pod =cut

sub exists_extra {
  my ($self, $object, $id, $name) = @_;
  my %extra = $self->get_all_detailed_extra($object, $id);

  return exists $extra{ $name };
}

#pod =head2 delete_all_extra
#pod
#pod   $driver->delete_all_extra($object, $id);
#pod
#pod This method deletes all extras for the object, as per the C<delete_extra>
#pod method.
#pod
#pod =cut

sub delete_all_extra {
  my ($self, $object, $id) = @_;

  for my $name ($self->get_all_extra_names($object, $id)) {
    $self->delete_extra($object, $id, $name);
  }
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Mixin::ExtraFields::Driver - a backend for extra field storage

=head1 VERSION

version 0.140003

=head1 SYNOPSIS

This is really not something you'd use on your own, it's just used by
Mixin::ExtraFields, but if you insist...

  my $driver = Mixin::ExtraFields::Driver::Phlogiston->from_args(\%arg);

  $driver->set($obj, $obj_id, flammable => "very!");

=head1 DESCRIPTION

Mixin::ExtraFields::Driver is a base class for drivers used by
Mixin::ExtraFields -- hence the name.  A driver is expected to store and
retrieve data keyed to an object and a name or key.  It can store this in any
way it likes, and does not need to guarantee persistence across processes.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should work
on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to lower
the minimum required perl.

=head1 SUBCLASSING

All drivers must implement the four methods listed below.  The base class has
implementations of these methods which will die noisily (C<confess>-ing) when
called.

Almost all methods are passed the same data as their first two arguments:
C<$object>, the object for which the driver is to find or alter data, and
C<$id>, that object's unique id.  While this may be slighly redundant, it keeps
the id-finding call in one place.

=head2 from_args

  my $driver = Mixin::ExtraFields::Driver::Subclass->from_args(\%arg);

This method must return a driver object appropriate to the given args.  It is
not called C<new> because it need not return a new object for each call to it.
Returning identical objects for identical configurations may be safe for some
driver implementations, and it is expressly allowed.

The arguments passed to this method are those given as the C<driver> option to
the C<fields> import group in Mixin::ExtraFields, less the C<class> option.

=head2 get_all_detailed_extra

  my %extra = $driver->get_all_detailed_extra($object, $id);

This method must return all available information about all existing extra
fields for the given object.  It must be returned as a list of name/value
pairs.  The values must be references to hashes.  Each hash must have an entry
for the key C<value> giving the value for that name.

=head2 set_extra

  $driver->set_extra($object, $id, $name, $value);

This method must set the named extra to the given value.

=head2 delete_extra

  $driver->delete_extra($object, $id, $name);

This method must delete the named extra, causing it to cease to exist.

=head1 OPTIMIZING

The methods below can all be implemented in terms of those above.  If they are
not provided by the subclass, basic implementations exist.  These
implementations may be less efficient than implementations crafted for the
specifics of the storage engine behind the driver, so authors of driver
subclasses should consider implementing these methods.

=head2 get_all_extra

  my %extra = $driver->get_all_extra($object, $id);

This method behaves like C<get_all_detailed_extra>, above, but provides the
entry's value, not a detailed hashref, as the value for each entry.

=head2 get_extra

=head2 get_detailed_extra

  my $value = $driver->get_extra($object, $id, $name);

  my $hash = $driver->get_detailed_extra($object, $id, $name);

These methods return a single value requested by name, either as the value
itself or a detailed hashref describing it.

=head2 get_all_extra_names

  my @names = $driver->get_all_extra_names($object, $id);

This method returns the names of all existing extras for the given object.

=head2 exists_extra

  if ($driver->exists_extra($object, $id, $name)) { ... }

This method returns true if an entry exists for the given name and false
otherwise.

=head2 delete_all_extra

  $driver->delete_all_extra($object, $id);

This method deletes all extras for the object, as per the C<delete_extra>
method.

=head1 AUTHOR

Ricardo Signes <cpan@semiotic.systems>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut