summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/AttributeTrait/Ranges.pm
blob: a629f11118585dc443c4a41f4c2dcc00f00689d3 (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
package Data::MultiValued::AttributeTrait::Ranges; 
use Moose::Role;
use namespace::autoclean;
use Data::MultiValued::Ranges;
with 'Data::MultiValued::AttributeTrait';
 
# ABSTRACT: attribute traits for attributes holding ranged values 
 
=head1 SYNOPSIS
 
  package My::Class;
  use Moose;
  use Data::MultiValued::AttributeTrait::Ranges;
 
  has stuff => (
    is => 'rw',
    isa => 'Int',
    traits => ['MultiValued::Ranges'],
    predicate => 'has_stuff',
    multi_accessor => 'stuff_tagged',
    multi_predicate => 'has_stuff_tagged',
  );
 
=head1 DESCRIPTION
 
This role consumes L<Data::MultiValued::AttributeTrait> and
specialises it to use L<Data::MultiValued::Ranges> as multi-value
storage:
 
=head2 C<multivalue_storage_class>
 
Returns C<'Data::MultiValued::Ranges'>.
 
=head2 C<opts_to_pass_set>
 
Returns C<('from', 'to')>.
 
=head2 C<opts_to_pass_get>
 
Returns C<('at')>.
 
=head2 C<all_ranges>
 
  my @ranges = $obj->meta->get_attribute('my_attr')->all_ranges($obj);
 
Returns a list of 2-element arrayrefs, each arrayref describing the
extremes of a range. Something like:
 
   [ [undef,10], [10,20], [20,undef] ]
 
=cut
 
sub multivalue_storage_class 'Data::MultiValued::Ranges' };
sub opts_to_pass_set qw(from to) }
sub opts_to_pass_get qw(at) }
 
sub all_ranges {
    my ($self,$instance) = @_;
 
    my $storage = $self->get_full_storage($instance);
    return unless $storage;
 
    return $storage->_storage->all_ranges;
}
 
package Moose::Meta::Attribute::Custom::Trait::MultiValued::Ranges;{ 
sub register_implementation 'Data::MultiValued::AttributeTrait::Ranges' }
}
 
1;