summaryrefslogtreecommitdiff
path: root/Data-MultiValued/lib/Data/MultiValued/AttributeTrait/Tagged.pm
blob: 7839e2d213aa458c0ae02c11402fe63fbb314373 (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
package Data::MultiValued::AttributeTrait::Tagged; 
use Moose::Role;
use Data::MultiValued::Tags;
use Data::MultiValued::AttributeAccessors;
use MooseX::Types::Moose qw(Str HashRef);
use Try::Tiny;
use namespace::autoclean;
 
has 'full_storage_slot' => (
    is => 'ro',
    isa => Str,
    lazy_build => 1,
    init_arg => undef,
);
sub _build_full_storage_slot shift->name . '__MULTIVALUED_STORAGE__' }
 
around slots => sub {
    my ($orig$self) = @_;
    return ($self->$orig(), $self->full_storage_slot);
};
 
sub set_full_storage {
    my ($self,$instance) = @_;
 
    my $ret = Data::MultiValued::Tags->new();
    $self->associated_class->get_meta_instance->set_slot_value(
        $instance,
        $self->full_storage_slot,
        $ret,
    );
    return $ret;
}
 
sub get_full_storage {
    my ($self,$instance) = @_;
 
    return $self->associated_class->get_meta_instance
        ->get_slot_value(
            $instance,
            $self->full_storage_slot,
        );
}
 
sub full_storage {
    my ($self,$instance) = @_;
 
    return $self->get_full_storage($instance)
        || $self->set_full_storage($instance);
}
 
sub accessor_metaclass 'Data::MultiValued::AttributeAccessors' }
 
after install_accessors => sub {
    my ($self) = @_;
 
    my $class  = $self->associated_class;
 
    for my $meth (qw(accessor reader writer predicate clearer)) {
        my $check = "has_$meth";
        next unless $self->$check;
 
        my $type = "tagged_$meth";
        my $basename = $self->$meth;
 
        die 'MultiValued attribute trait is not compatible with subref accessors'
            if ref($basename);
 
        my $name = "${basename}_tagged";
 
        $class->add_method(
            $self->_process_accessors($type => $name,0)
        );
    }
};
 
sub load_tagged_value {
    my ($self,$instance,$opts) = @_;
 
    my $value;my $found=1;
    try {
        $value = $self->full_storage($instance)->get($opts);
    }
    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);
    }
}
 
sub raw_clear_value {
    my ($self,$instance) = @_;
 
    $self->associated_class->get_meta_instance
        ->deinitialize_slot(
            $instance,
            $self->name,
        );
}
 
sub store_tagged_value {
    my ($self,$instance,$opts) = @_;
 
    my $value = $self->get_raw_value($instance);
    $self->full_storage($instance)->set({%$opts,value=>$value});
}
 
our $dyn_opts = {};
 
before get_value => sub {
    my ($self,$instance) = @_;
 
    $self->load_tagged_value($instance,$dyn_opts);
};
 
sub get_tagged_value {
    my ($self,$instance,$opts,$value) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->get_value($instance,$value);
}
 
after set_initial_value => sub {
    my ($self,$instance,$value) = @_;
 
    $self->store_tagged_value($instance,$dyn_opts);
};
 
after set_value => sub {
    my ($self,$instance,$value) = @_;
 
    $self->store_tagged_value($instance,$dyn_opts);
};
 
sub set_tagged_value {
    my ($self,$instance,$opts,$value) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->set_value($instance,$value);
}
 
before has_value => sub {
    my ($self,$instance) = @_;
 
    $self->load_tagged_value($instance,$dyn_opts);
};
 
sub has_tagged_value {
    my ($self,$instance,$opts) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->has_value($instance);
}
 
after clear_value => sub {
    my ($self,$instance) = @_;
 
    # XXX NIY 
    $self->full_storage($instance)->clear($dyn_opts);
};
 
sub clear_tagged_value {
    my ($self,$instance,$opts) = @_;
 
    local $dyn_opts = $opts;
 
    return $self->clear_value($instance);
}
 
sub get_tagged_read_method  
    my $self   = shift;
    return $self->get_read_method . '_tagged';
}
 
sub get_tagged_write_method  
    my $self   = shift;
    return $self->get_write_method . '_tagged';
}
 
package Moose::Meta::Attribute::Custom::Trait::MultiValued::Tagged;{ 
sub register_implementation 'Data::MultiValued::AttributeTrait::Tagged' }
}
 
1;