summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/AttributeTrait.pm
blob: eb8cbd32eeab43780eeec86ee542f6c6119eb530 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package Data::MultiValued::AttributeTrait; 
use Moose::Role;
use namespace::autoclean;
use Data::MultiValued::AttributeAccessors;
use MooseX::Types::Moose qw(Str);
use Try::Tiny;
use namespace::autoclean;
 
# ABSTRACT: "base role" for traits of multi-valued Moose attributes 
 
=head1 DESCRIPTION
 
Don't use this role directly, use
L<Data::MultiValued::AttributeTrait::Tags>,
L<Data::MultiValued::AttributeTrait::Ranges> or
L<Data::MultiValued::AttributeTrait::TagsAndRanges>.
 
This role (together with L<Data::MultiValued::AttributeAccessors>)
defines all the basic plumbing to glue C<Data::MultiValued::Tags> etc
into Moose attributes.
 
=head2 Implementation details
 
The multi-value object is stored in the instance slot named by the
L</full_storage_slot> attribute attribute. C<before> modifiers on
getters load the appropriate value from the multi-value object into
the regular instance slot, C<after> modifiers on setters store the
value from the regular instance slot into the multi-value object.
 
=head2 Attributes
 
This trait adds some attributes to the attribute declarations in your
class. Example:
 
  has stuff => (
    is => 'rw',
    isa => 'Int',
    traits => ['MultiValued::Tags'],
    predicate => 'has_stuff',
    multi_accessor => 'stuff_tagged',
    multi_predicate => 'has_stuff_tagged',
  );
 
=attr C<full_storage_slot>
 
The instance slot to use to store the C<Data::MultiValued::Tags> or
similar object. Defaults to C<"${name}__MULTIVALUED_STORAGE__">, where
C<$name> is the attribute name.
 
=cut
 
has 'full_storage_slot' => (
    is => 'ro',
    isa => Str,
    lazy_build => 1,
);
sub _build_full_storage_slot shift->name . '__MULTIVALUED_STORAGE__' }
 
=attr C<multi_accessor>
 
=attr C<multi_reader>
 
=attr C<multi_writer>
 
=attr C<multi_predicate>
 
=attr C<multi_clearer>
 
The names to use for the various additional accessors. See
L<Class::MOP::Attribute> for details. These default to
C<"${name}_multi"> where C<$name> is the name of the corresponding
non-multi accessor. So, for example,
 
  has stuff => (
    is => 'rw',
    traits => ['MultiValued::Tags'],
  );
 
will create a C<stuff> read / write accessor and a C<stuff_multi> read
/ write tagged accessor.
 
=cut
 
my @accs_to_multiply=qw(accessor reader writer predicate clearer);
 
for my $acc (@accs_to_multiply) {
    has "multi_$acc" => (
        is => 'ro',
        isa => Str,
        predicate => "has_multi_$acc",
    );
}
 
=head1 REQUIREMENTS
 
These methods must be provided by any class consuming this role. See
L<Data::MultiValued::AttributeTrait::Tags> etc. for examples.
 
=head2 C<multivalue_storage_class>
 
The class to use to create the multi-value objects.
 
=cut
 
requires 'multivalue_storage_class';
 
=head2 C<opts_to_pass_set>
 
Which options to pass from the multi-value accessors to the C<set>
method of the multi-value object.
 
=cut
 
requires 'opts_to_pass_set';
 
=head2 C<opts_to_pass_get>
 
Which options to pass from the multi-value accessors to the C<get>
method of the multi-value object.
 
=cut
 
requires 'opts_to_pass_get';
 
=method C<slots>
 
Adds the L</full_storage_slot> to the list of used slots.
 
=cut
 
around slots => sub {
    my ($orig$self) = @_;
    return ($self->$orig(), $self->full_storage_slot);
};
 
=method C<set_full_storage>
 
Stores a new instance of L</multivalue_storage_class> into the
L</full_storage_slot> of the instance.
 
=cut
 
sub set_full_storage {
    my ($self,$instance) = @_;
 
    my $ret = $self->multivalue_storage_class->new();
    $self->associated_class->get_meta_instance->set_slot_value(
        $instance,
        $self->full_storage_slot,
        $ret,
    );
    return $ret;
}
 
=method C<get_full_storage>
 
Retrieves the value of the L</full_storage_slot> of the instance.
 
=cut
 
sub get_full_storage {
    my ($self,$instance) = @_;
 
    return $self->associated_class->get_meta_instance
        ->get_slot_value(
            $instance,
            $self->full_storage_slot,
        );
}
 
=method C<full_storage>
 
Returns an instance of L</multivalue_storage_class>, either by
retrieving it from the instance, or by creating one (and setting it in
the instance). Calls L</get_full_storage> and L</set_full_storage>.
 
=cut
 
sub full_storage {
    my ($self,$instance) = @_;
 
    return $self->get_full_storage($instance)
        || $self->set_full_storage($instance);
}
 
=method C<accessor_metaclass>
 
Makes sure that all accessors for this attribute are created via the
L<Data::MultiValued::AttributeAccessors> method meta class.
 
=cut
 
sub accessor_metaclass 'Data::MultiValued::AttributeAccessors' }
 
=method C<install_accessors>
 
After the regular L<Moose::Meta::Attribute> method, installs the
multi-value accessors.
 
Each installed normal accessor gets a multi-value version
 
You can add or rename the multi-value version by using the attributes
described above
 
If you are passing explicit subrefs for your accessors, things won't work.
 
=cut
 
after install_accessors => sub {
    my ($self) = @_;
 
    my $class  = $self->associated_class;
 
    for my $meth (@accs_to_multiply) {
        my $type = "multi_$meth";
        my $check = "has_$meth";
        my $multi_check = "has_$type";
        next unless $self->$check || $self->$multi_check;
 
        my $name = $self->$type;
        if (!$name) {
            my $basename = $self->$meth;
 
            die 'MultiValued attribute trait is not compatible with subref accessors'
                if ref($basename);
 
            $name = "${basename}_multi";
        }
 
        $class->add_method(
            $self->_process_accessors($type => $name,0)
        );
    }
};
 
sub _filter_opts {
    my ($hr,@fields) = @_;
 
    my %ret;
    for my $f (@fields) {
        if (exists $hr->{$f}) {
            $ret{$f}=$hr->{$f};
        }
    }
    return \%ret;
}
 
=method C<load_multi_value>
 
Retrieves a value from the multi-value object, and stores it in the
regular slot in the instance. If the value is not found, clears the
slot.
 
This traps the
L<Data::MultiValued::Exceptions::NotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::NotFound>
exception that may be thrown by the multi-value object, but re-throws
any other exception.
 
=cut
 
sub load_multi_value {
    my ($self,$instance,$opts) = @_;
 
    my $opts_passed = _filter_opts($opts$self->opts_to_pass_get);
 
    my $value;my $found=1;
    try {
        $value = $self->full_storage($instance)->get($opts_passed);
    }
    catch {
        unless (ref($_) && $_->isa('Data::MultiValued::Exceptions::NotFound')) {
            die $_;
        }
        $found = 0;
    };
 
    if ($found) {
        $self->set_raw_value($instance,$value);
    }
    else {
        $self->raw_clear_value($instance);
    }
}
 
=method C<raw_clear_value>
 
Clears the instance slot. Does the same as
L<Moose::Meta::Attribute/clear_value>, but we need this method because
the other one gets changed by this trait.
 
=cut
 
sub raw_clear_value {
    my ($self,$instance) = @_;
 
    $self->associated_class->get_meta_instance
        ->deinitialize_slot(
            $instance,
            $self->name,
        );
}
 
=method C<store_multi_value>
 
Gets the value from the regular slot in the instance, and stores it
into the multi-value object.
 
=cut
 
sub store_multi_value {
    my ($self,$instance,$opts) = @_;
 
    my $opts_passed = _filter_opts($opts$self->opts_to_pass_set);
 
    $opts_passed->{value} = $self->get_raw_value($instance);
 
    $self->full_storage($instance)->set($opts_passed);
}
 
our $dyn_opts = {};
 
=method C<get_value>
 
Before the normal method, calls L</load_multi_value>. Normally, no
options will be passed to the multi-value object C<get> method.
 
=cut
 
before get_value => sub {
    my ($self,$instance) = @_;
 
    $self->load_multi_value($instance,$dyn_opts);
};
 
=method C<get_multi_value>
 
Sets the options that L</load_multi_value> will use, then calls L</get_value>.
 
The options are passed via an ugly C<local>ised package
variable. There might be a better way.
 
=cut
 
sub get_multi_value {
    my ($self,$instance,$opts) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->get_value($instance);
}
 
=method C<set_initial_value>
 
After the normal method, calls L</store_multi_value>.
 
=cut
 
after set_initial_value => sub {
    my ($self,$instance,$value) = @_;
 
    $self->store_multi_value($instance,$dyn_opts);
};
 
=method C<set_value>
 
=method C<set_multi_value>
 
Just like L</get_value> and L</get_multi_value>, but calling
L</store_multi_value> after the regular C<set_value>
 
=cut
 
after set_value => sub {
    my ($self,$instance,$value) = @_;
 
    $self->store_multi_value($instance,$dyn_opts);
};
 
sub set_multi_value {
    my ($self,$instance,$opts,$value) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->set_value($instance,$value);
}
 
=method C<has_value>
 
=method C<has_multi_value>
 
Just like L</get_value> and L</get_multi_value>.
 
=cut
 
before has_value => sub {
    my ($self,$instance) = @_;
 
    $self->load_multi_value($instance,$dyn_opts);
};
 
sub has_multi_value {
    my ($self,$instance,$opts) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->has_value($instance);
}
 
=method C<clear_value>
 
=method C<clear_multi_value>
 
Call the C<clear> method on the multi-value object.
 
=cut
 
after clear_value => sub {
    my ($self,$instance) = @_;
 
    $self->full_storage($instance)->clear($dyn_opts);
    return;
};
 
sub clear_multi_value {
    my ($self,$instance,$opts) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->clear_value($instance);
}
 
=method C<get_multi_read_method>
 
=method C<get_multi_write_method>
 
Return the name of the reader or writer method, honoring
L</multi_reader>, L</multi_writer> and L</multi_accessor>.
 
=cut
 
sub get_multi_read_method  
    my $self   = shift;
    return $self->multi_reader || $self->multi_accessor
        || $self->get_read_method . '_multi';
}
 
sub get_multi_write_method  
    my $self   = shift;
    return $self->multi_writer || $self->multi_accessor
        || $self->get_write_method . '_multi';
}
 
=head1 Serialisation helpers
 
These are used through
L<Data::MultiValued::UglySerializationHelperRole>.
 
=head2 C<_rebless_slot>
 
Blesses the value inside the L</full_storage_slot> of the instance
into L</multivalue_storage_class>, then calls C<_rebless_storage> on
it.
 
=cut
 
sub _rebless_slot {
    my ($self,$instance) = @_;
 
    my $st = $self->get_full_storage($instance);
    return unless $st;
 
    bless $st$self->multivalue_storage_class;
    $st->_rebless_storage;
}
 
=head2 C<_as_hash>
 
Returns the result of calling C<_as_hash> on the value inside the
L</full_storage_slot> of the instance. Returns nothing if the slot
does not have a value.
 
=cut
 
sub _as_hash {
    my ($self,$instance) = @_;
 
    my $st = $self->get_full_storage($instance);
    return unless $st;
 
    return $st->_as_hash;
}
 
1;