| 1 | package MooseX::RememberHistory::Trait::Attribute; | ||||||||||
| 2 | { | ||||||||||
| 3 | 1 | $MooseX::RememberHistory::Trait::Attribute::VERSION = '0.001'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | has 'history_getter' => ( | |||||||||
| 9 | isa => 'Str', | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | lazy => 1, | ||||||||||
| 12 | builder => '_history_name', | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | sub _history_name { | ||||||||
|
Subroutine
_history_name: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | my $attr = shift; | |||||||||
| 17 | 1 | my $name = $attr->name; | |||||||||
| 18 | 1 | return $name . '_history'; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 2 | ▶ | around 'install_accessors' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | my $orig = shift; | |||||||||
| 23 | 2 | my $attr = shift; | |||||||||
| 24 | |||||||||||
| 25 | 2 | my $class = $attr->associated_class; | |||||||||
| 26 | 2 | my $hist_name = $attr->history_getter; | |||||||||
| 27 | |||||||||||
| 28 | #add history holder | ||||||||||
| 29 | $class->add_attribute( | ||||||||||
| 30 | $hist_name => ( | ||||||||||
| 31 | is => 'rw', | ||||||||||
| 32 | isa => 'ArrayRef', | ||||||||||
| 33 | 4 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ), | |||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 2 | $attr->$orig(@_); | |||||||||
| 38 | |||||||||||
| 39 | # sync history on first access | ||||||||||
| 40 | 5 | ▶ | $class->add_around_method_modifier($hist_name, sub{ | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 41 | 5 | my $orig = shift; | |||||||||
| 42 | 5 | my $self = shift; | |||||||||
| 43 | 5 | my $history = $self->$orig(@_); | |||||||||
| 44 | 5 | ▶ | unless (@$history) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 3 | my $old_val = $attr->get_value($self); | |||||||||
| 46 | 3 | ▶ | push @$history, $old_val if defined $old_val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | } | ||||||||||
| 48 | 5 | return $history; | |||||||||
| 49 | 2 | }); | |||||||||
| 50 | |||||||||||
| 51 | # push on to history on write | ||||||||||
| 52 | 2 | my $writer_name = $attr->get_write_method; | |||||||||
| 53 | 4 | ▶ | $class->add_before_method_modifier($writer_name, sub{ | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 54 | 4 | my ($self, $value) = @_; | |||||||||
| 55 | 4 | ▶ | return unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 2 | my $history = $self->can($hist_name)->($self); | |||||||||
| 57 | 2 | push @$history, $value; | |||||||||
| 58 | 2 | }); | |||||||||
| 59 | 1 | }; | |||||||||
| 60 | |||||||||||
| 61 | 1 | 1; | |||||||||
| 62 | |||||||||||