| 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::V1::Trait::Attribute; | ||||||||||||
| 11 | { | ||||||||||||
| 12 | 1 | $MooseX::AutoDestruct::V1::Trait::Attribute::VERSION = '0.009'; | |||||||||||
| 13 | } | ||||||||||||
| 14 | |||||||||||||
| 15 | # ABSTRACT: Moose 1.x autodestruct traits | ||||||||||||
| 16 | |||||||||||||
| 17 | 1 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 1 | has ttl => (is => 'ro', isa => 'Int', required => 1, predicate => 'has_ttl'); | |||||||||||
| 21 | |||||||||||||
| 22 | # generate / store our metaclass | ||||||||||||
| 23 | 1 | has _accessor_metaclass => (is => 'rw', isa => 'Str', predicate => '_has_accessor_metaclass'); | |||||||||||
| 24 | |||||||||||||
| 25 | 0 | ▶ | around accessor_metaclass => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 26 | 0 | my ($orig, $self) = (shift, shift); | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | return $self->_accessor_metaclass if $self->_has_accessor_metaclass; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | |||||||||||||
| 30 | # get our base metaclass... | ||||||||||||
| 31 | 0 | my $base_class = $self->$orig(); | |||||||||||
| 32 | |||||||||||||
| 33 | # ...and apply our trait to it | ||||||||||||
| 34 | ### superclasses: $base_class->meta->name | ||||||||||||
| 35 | 0 | my $new_class_meta = Moose::Meta::Class->create_anon_class( | |||||||||||
| 36 | superclasses => [ $base_class->meta->name ], | ||||||||||||
| 37 | roles => [ 'MooseX::AutoDestruct::V1::Trait::Method::Accessor' ], | ||||||||||||
| 38 | cache => 1, | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | ### new accessor class: $new_class_meta->name | ||||||||||||
| 42 | 0 | $self->_accessor_metaclass($new_class_meta->name); | |||||||||||
| 43 | 0 | return $new_class_meta->name; | |||||||||||
| 44 | 1 | }; | |||||||||||
| 45 | |||||||||||||
| 46 | 1 | has value_slot => (is => 'ro', isa => 'Str', lazy_build => 1, init_arg => undef); | |||||||||||
| 47 | 1 | has destruct_at_slot => (is => 'ro', isa => 'Str', lazy_build => 1, init_arg => undef); | |||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub _build_value_slot { shift->name } | ||||||||||
|
Subroutine
_build_value_slot: not called CC=1 SCAR=6.9
| |||||||||||||
| 50 | 0 | ▶ | sub _build_destruct_at_slot { shift->name . '__DESTRUCT_AT__' } | ||||||||||
|
Subroutine
_build_destruct_at_slot: not called CC=1 SCAR=6.9
| |||||||||||||
| 51 | |||||||||||||
| 52 | 0 | ▶ | around slots => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 53 | 0 | my ($orig, $self) = (shift, shift); | |||||||||||
| 54 | |||||||||||||
| 55 | 0 | my $base = $self->$orig(); | |||||||||||
| 56 | 0 | return ($self->$orig(), $self->destruct_at_slot); | |||||||||||
| 57 | 1 | }; | |||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | sub set_doomsday { | ||||||||||
|
Subroutine
set_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 60 | 0 | my ($self, $instance) = @_; | |||||||||||
| 61 | |||||||||||||
| 62 | # ... | ||||||||||||
| 63 | |||||||||||||
| 64 | # set our destruct_at slot | ||||||||||||
| 65 | 0 | my $doomsday = $self->ttl + time; | |||||||||||
| 66 | |||||||||||||
| 67 | ### doomsday set to: $doomsday | ||||||||||||
| 68 | ### time() is: time() | ||||||||||||
| 69 | 0 | $self | |||||||||||
| 70 | ->associated_class | ||||||||||||
| 71 | ->get_meta_instance | ||||||||||||
| 72 | ->set_slot_value($instance, $self->destruct_at_slot, $doomsday) | ||||||||||||
| 73 | ; | ||||||||||||
| 74 | |||||||||||||
| 75 | 0 | return; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 0 | ▶ | sub has_doomsday { | ||||||||||
|
Subroutine
has_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 79 | 0 | my ($self, $instance) = @_; | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | return $self | |||||||||||
| 82 | ->associated_class | ||||||||||||
| 83 | ->get_meta_instance | ||||||||||||
| 84 | ->is_slot_initialized($instance, $self->destruct_at_slot) | ||||||||||||
| 85 | ; | ||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | # return true if this value has expired | ||||||||||||
| 89 | 0 | ▶ | sub doomsday { | ||||||||||
|
Subroutine
doomsday: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 90 | 0 | my ($self, $instance) = @_; | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $doomsday = $self | |||||||||||
| 93 | ->associated_class | ||||||||||||
| 94 | ->get_meta_instance | ||||||||||||
| 95 | ->get_slot_value($instance, $self->destruct_at_slot) | ||||||||||||
| 96 | ; | ||||||||||||
| 97 | 0 | ▶ | $doomsday ||= 0; | ||||||||||
|
Condition0%0/2
$doomsday ||= 0
MC/DC0%0/1
$doomsday ||= 0
$doomsday
Truth table0%0/2
A
A= $doomsday
| |||||||||||||
| 98 | |||||||||||||
| 99 | ### $doomsday | ||||||||||||
| 100 | ### time > $doomsday: time > $doomsday | ||||||||||||
| 101 | 0 | return time > $doomsday; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 0 | ▶ | sub avert_doomsday { | ||||||||||
|
Subroutine
avert_doomsday: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 105 | 0 | my ($self, $instance) = @_; | |||||||||||
| 106 | |||||||||||||
| 107 | ### in avert_doomsday()... | ||||||||||||
| 108 | 0 | $self | |||||||||||
| 109 | ->associated_class | ||||||||||||
| 110 | ->get_meta_instance | ||||||||||||
| 111 | ->deinitialize_slot($instance, $self->destruct_at_slot) | ||||||||||||
| 112 | ; | ||||||||||||
| 113 | |||||||||||||
| 114 | 0 | return; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | after set_initial_value => sub { shift->set_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 118 | 0 | ▶ | after set_value => sub { shift->set_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 119 | 0 | ▶ | after clear_value => sub { shift->avert_doomsday(shift) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | before get_value => sub { shift->enforce_doomsday(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 122 | 0 | ▶ | before has_value => sub { shift->enforce_doomsday(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 123 | |||||||||||||
| 124 | 0 | ▶ | sub enforce_doomsday { | ||||||||||
|
Subroutine
enforce_doomsday: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 125 | 0 | my ($self, $instance, $for_trigger) = @_; | |||||||||||
| 126 | |||||||||||||
| 127 | # if we're not set yet... | ||||||||||||
| 128 | 0 | ▶ | $self->clear_value($instance) if $self->doomsday($instance); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 0 | return; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | # FIXME do we need this? | ||||||||||||
| 133 | 0 | ▶ | after get_value => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 134 | 0 | my ($self, $instance, $for_trigger) = @_; | |||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | $self->set_doomsday unless $self->has_doomsday($instance); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 1 | }; | |||||||||||
| 138 | |||||||||||||
| 139 | 1 | !!42; | |||||||||||
| 140 | |||||||||||||
| 141 | |||||||||||||
| 142 | |||||||||||||
| 143 | =pod | ||||||||||||
| 144 | |||||||||||||
| 145 | =head1 NAME | ||||||||||||
| 146 | |||||||||||||
| 147 | MooseX::AutoDestruct::V1::Trait::Attribute - Moose 1.x autodestruct traits | ||||||||||||
| 148 | |||||||||||||
| 149 | =head1 VERSION | ||||||||||||
| 150 | |||||||||||||
| 151 | version 0.009 | ||||||||||||
| 152 | |||||||||||||
| 153 | =head1 DESCRIPTION | ||||||||||||
| 154 | |||||||||||||
| 155 | Implementation attribute trait of L<MooseX::AutoDestruct> for L<Moose> 1.xx. | ||||||||||||
| 156 | |||||||||||||
| 157 | =begin Pod::Coverage | ||||||||||||
| 158 | |||||||||||||
| 159 | |||||||||||||
| 160 | |||||||||||||
| 161 | |||||||||||||
| 162 | =end Pod::Coverage | ||||||||||||
| 163 | |||||||||||||
| 164 | =head1 SEE ALSO | ||||||||||||
| 165 | |||||||||||||
| 166 | L<MooseX:AutoDestruct>. | ||||||||||||
| 167 | |||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||
| 169 | |||||||||||||
| 170 | Chris Weyl <cweyl@alumni.drew.edu> | ||||||||||||
| 171 | |||||||||||||
| 172 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 173 | |||||||||||||
| 174 | This software is Copyright (c) 2011 by Chris Weyl. | ||||||||||||
| 175 | |||||||||||||
| 176 | This is free software, licensed under: | ||||||||||||
| 177 | |||||||||||||
| 178 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||
| 179 | |||||||||||||
| 180 | =cut | ||||||||||||
| 181 | |||||||||||||
| 182 | |||||||||||||
| 183 | __END__ | ||||||||||||