| 1 | # | ||||||||||||
| 2 | # This file is part of MooseX-AutoDestruct | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # This software is Copyright (c) 2011 by Chris Weyl. | ||||||||||||
| 5 | # | ||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||
| 7 | # | ||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||
| 9 | # | ||||||||||||
| 10 | package MooseX::AutoDestruct::V2::Trait::Attribute; | ||||||||||||
| 11 | { | ||||||||||||
| 12 | 4 | $MooseX::AutoDestruct::V2::Trait::Attribute::VERSION = '0.009'; | |||||||||||
| 13 | } | ||||||||||||
| 14 | |||||||||||||
| 15 | # ABSTRACT: Clear your attributes after a certain time | ||||||||||||
| 16 | |||||||||||||
| 17 | 4 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 4 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | # debugging | ||||||||||||
| 21 | #use Smart::Comments '###', '####'; | ||||||||||||
| 22 | |||||||||||||
| 23 | 4 | has ttl => (is => 'ro', isa => 'Int', required => 1, predicate => 'has_ttl'); | |||||||||||
| 24 | |||||||||||||
| 25 | 4 | has value_slot => (is => 'ro', isa => 'Str', lazy_build => 1, init_arg => undef); | |||||||||||
| 26 | 4 | has destruct_at_slot => (is => 'ro', isa => 'Str', lazy_build => 1, init_arg => undef); | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub _build_value_slot { shift->name } | ||||||||||
|
Subroutine
_build_value_slot: not called CC=1 SCAR=6.9
| |||||||||||||
| 29 | 2 | ▶ | sub _build_destruct_at_slot { shift->name . '__DESTRUCT_AT__' } | ||||||||||
|
Subroutine
_build_destruct_at_slot: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | |||||||||||||
| 31 | 2 | ▶ | around slots => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 2 | my ($orig, $self) = (shift, shift); | |||||||||||
| 33 | |||||||||||||
| 34 | 2 | return ($self->$orig(), $self->destruct_at_slot); | |||||||||||
| 35 | 4 | }; | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | ▶ | sub set_doomsday { | ||||||||||
|
Subroutine
set_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 38 | 0 | my ($self, $instance) = @_; | |||||||||||
| 39 | |||||||||||||
| 40 | # ... | ||||||||||||
| 41 | |||||||||||||
| 42 | # set our destruct_at slot | ||||||||||||
| 43 | 0 | my $doomsday = $self->ttl + time; | |||||||||||
| 44 | |||||||||||||
| 45 | ### doomsday set to: $doomsday | ||||||||||||
| 46 | ### time() is: time() | ||||||||||||
| 47 | 0 | $self | |||||||||||
| 48 | ->associated_class | ||||||||||||
| 49 | ->get_meta_instance | ||||||||||||
| 50 | ->set_slot_value($instance, $self->destruct_at_slot, $doomsday) | ||||||||||||
| 51 | ; | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | return; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | sub has_doomsday { | ||||||||||
|
Subroutine
has_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 57 | 0 | my ($self, $instance) = @_; | |||||||||||
| 58 | |||||||||||||
| 59 | 0 | return $self | |||||||||||
| 60 | ->associated_class | ||||||||||||
| 61 | ->get_meta_instance | ||||||||||||
| 62 | ->is_slot_initialized($instance, $self->destruct_at_slot) | ||||||||||||
| 63 | ; | ||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | # return true if this value has expired | ||||||||||||
| 67 | 0 | ▶ | sub doomsday { | ||||||||||
|
Subroutine
doomsday: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 68 | 0 | my ($self, $instance) = @_; | |||||||||||
| 69 | |||||||||||||
| 70 | 0 | my $doomsday = $self | |||||||||||
| 71 | ->associated_class | ||||||||||||
| 72 | ->get_meta_instance | ||||||||||||
| 73 | ->get_slot_value($instance, $self->destruct_at_slot) | ||||||||||||
| 74 | ; | ||||||||||||
| 75 | 0 | ▶ | $doomsday ||= 0; | ||||||||||
|
Condition0%0/2
$doomsday ||= 0
MC/DC0%0/1
$doomsday ||= 0
$doomsday
Truth table0%0/2
A
A= $doomsday
| |||||||||||||
| 76 | |||||||||||||
| 77 | ### $doomsday | ||||||||||||
| 78 | ### time > $doomsday: time > $doomsday | ||||||||||||
| 79 | 0 | return time > $doomsday; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | ▶ | sub avert_doomsday { | ||||||||||
|
Subroutine
avert_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 83 | 0 | my ($self, $instance) = @_; | |||||||||||
| 84 | |||||||||||||
| 85 | ### in avert_doomsday()... | ||||||||||||
| 86 | 0 | $self | |||||||||||
| 87 | ->associated_class | ||||||||||||
| 88 | ->get_meta_instance | ||||||||||||
| 89 | ->deinitialize_slot($instance, $self->destruct_at_slot) | ||||||||||||
| 90 | ; | ||||||||||||
| 91 | |||||||||||||
| 92 | 0 | return; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | after set_initial_value => sub { shift->set_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 96 | 0 | ▶ | after set_value => sub { shift->set_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 97 | 0 | ▶ | after clear_value => sub { shift->avert_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | before get_value => sub { shift->enforce_doomsday(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 100 | 0 | ▶ | before has_value => sub { shift->enforce_doomsday(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | sub enforce_doomsday { | ||||||||||
|
Subroutine
enforce_doomsday: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 103 | 0 | my ($self, $instance, $for_trigger) = @_; | |||||||||||
| 104 | |||||||||||||
| 105 | # if we're not set yet... | ||||||||||||
| 106 | 0 | ▶ | $self->clear_value($instance) if $self->doomsday($instance); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | return; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | # FIXME do we need this? | ||||||||||||
| 111 | 0 | ▶ | after get_value => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 112 | 0 | my ($self, $instance, $for_trigger) = @_; | |||||||||||
| 113 | |||||||||||||
| 114 | 0 | ▶ | $self->set_doomsday unless $self->has_doomsday($instance); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 4 | }; | |||||||||||
| 116 | |||||||||||||
| 117 | 3 | ▶ | around _inline_clear_value => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 118 | 3 | my ($orig, $self) = (shift, shift); | |||||||||||
| 119 | 3 | my ($instance) = @_; | |||||||||||
| 120 | |||||||||||||
| 121 | 3 | my $mi = $self->associated_class->get_meta_instance; | |||||||||||
| 122 | |||||||||||||
| 123 | 3 | return $self->$orig(@_) | |||||||||||
| 124 | . $mi->inline_deinitialize_slot($instance, $self->destruct_at_slot) | ||||||||||||
| 125 | . ';' | ||||||||||||
| 126 | ; | ||||||||||||
| 127 | 4 | }; | |||||||||||
| 128 | |||||||||||||
| 129 | 4 | ▶ | sub _inline_destruct { | ||||||||||
|
Subroutine
_inline_destruct: called CC=3 SCAR=11.0
| |||||||||||||
| 130 | 4 | my $self = shift; | |||||||||||
| 131 | 4 | my ($instance) = @_; | |||||||||||
| 132 | |||||||||||||
| 133 | 4 | my $slot_exists = $self->_inline_instance_has(@_); | |||||||||||
| 134 | 4 | my $destruct_at_slot_value = $self | |||||||||||
| 135 | ->associated_class | ||||||||||||
| 136 | ->get_meta_instance | ||||||||||||
| 137 | ->inline_get_slot_value($instance, $self->destruct_at_slot) | ||||||||||||
| 138 | ; | ||||||||||||
| 139 | |||||||||||||
| 140 | 4 | my $clear_attribute; | |||||||||||
| 141 | 4 | ▶ | if ($self->has_clearer) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | |||||||||||||
| 143 | # if we have a clearer method, we should call that -- it may have | ||||||||||||
| 144 | # been wrapped in the class | ||||||||||||
| 145 | |||||||||||||
| 146 | 2 | my $clearer = $self->clearer; | |||||||||||
| 147 | 2 | ▶ | ($clearer) = keys %$clearer if ref $clearer; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | |||||||||||||
| 149 | 2 | $clear_attribute = "${instance}->" . $clearer . '()'; | |||||||||||
| 150 | } | ||||||||||||
| 151 | else { | ||||||||||||
| 152 | # otherwise, just deinit all the slots we use | ||||||||||||
| 153 | 2 | $clear_attribute = $self->_inline_clear_value(@_); | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 4 | return " if ($slot_exists && time() > $destruct_at_slot_value) { $clear_attribute } "; | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | 4 | ▶ | my $destruct_wrapper = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 160 | 4 | my $self = shift; | |||||||||||
| 161 | 4 | return ($self->_inline_destruct(@_), super); | |||||||||||
| 162 | 4 | }; | |||||||||||
| 163 | |||||||||||||
| 164 | 4 | override _inline_has_value => $destruct_wrapper; | |||||||||||
| 165 | 4 | override _inline_get_value => $destruct_wrapper; | |||||||||||
| 166 | |||||||||||||
| 167 | 5 | ▶ | sub _inline_set_doomsday { | ||||||||||
|
Subroutine
_inline_set_doomsday: called CC=1 SCAR=0.0
| |||||||||||||
| 168 | 5 | my ($self, $instance) = @_; | |||||||||||
| 169 | 5 | my $mi = $self->associated_class->get_meta_instance; | |||||||||||
| 170 | |||||||||||||
| 171 | 5 | my $code = $mi->inline_set_slot_value( | |||||||||||
| 172 | $instance, | ||||||||||||
| 173 | $self->destruct_at_slot, | ||||||||||||
| 174 | 'time() + ' . $self->ttl, | ||||||||||||
| 175 | ); | ||||||||||||
| 176 | |||||||||||||
| 177 | 5 | return "$code;\n"; | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | 5 | ▶ | override _inline_instance_set => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 181 | 5 | my $self = shift; | |||||||||||
| 182 | 5 | return 'do { ' . $self->_inline_set_doomsday(@_) . ';' . super . ' }'; | |||||||||||
| 183 | 4 | }; | |||||||||||
| 184 | |||||||||||||
| 185 | 4 | !!42; | |||||||||||
| 186 | |||||||||||||
| 187 | |||||||||||||
| 188 | |||||||||||||
| 189 | =pod | ||||||||||||
| 190 | |||||||||||||
| 191 | =head1 NAME | ||||||||||||
| 192 | |||||||||||||
| 193 | MooseX::AutoDestruct::V2::Trait::Attribute - Clear your attributes after a certain time | ||||||||||||
| 194 | |||||||||||||
| 195 | =head1 VERSION | ||||||||||||
| 196 | |||||||||||||
| 197 | version 0.009 | ||||||||||||
| 198 | |||||||||||||
| 199 | =head1 DESCRIPTION | ||||||||||||
| 200 | |||||||||||||
| 201 | Attribute trait of L<MooseX::AutoDestruct> for L<Moose> version | ||||||||||||
| 202 | 2.xx. | ||||||||||||
| 203 | |||||||||||||
| 204 | =begin Pod::Coverage | ||||||||||||
| 205 | |||||||||||||
| 206 | |||||||||||||
| 207 | |||||||||||||
| 208 | |||||||||||||
| 209 | =end Pod::Coverage | ||||||||||||
| 210 | |||||||||||||
| 211 | =head1 SEE ALSO | ||||||||||||
| 212 | |||||||||||||
| 213 | L<MooseX:AutoDestruct>. | ||||||||||||
| 214 | |||||||||||||
| 215 | =head1 AUTHOR | ||||||||||||
| 216 | |||||||||||||
| 217 | Chris Weyl <cweyl@alumni.drew.edu> | ||||||||||||
| 218 | |||||||||||||
| 219 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 220 | |||||||||||||
| 221 | This software is Copyright (c) 2011 by Chris Weyl. | ||||||||||||
| 222 | |||||||||||||
| 223 | This is free software, licensed under: | ||||||||||||
| 224 | |||||||||||||
| 225 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||
| 226 | |||||||||||||
| 227 | =cut | ||||||||||||
| 228 | |||||||||||||
| 229 | |||||||||||||
| 230 | __END__ | ||||||||||||