| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | package Class::Workflow::Util::Delta; | ||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Data::Compare (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | has from => ( | |||||||||
| 11 | does => "Class::Workflow::Instance", | ||||||||||
| 12 | is => "ro", | ||||||||||
| 13 | required => 1, | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 1 | has to => ( | |||||||||
| 17 | does => "Class::Workflow::Instance", | ||||||||||
| 18 | is => "ro", | ||||||||||
| 19 | required => 1, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | has changes => ( | ||||||||||
| 23 | isa => "HashRef", | ||||||||||
| 24 | is => "ro", | ||||||||||
| 25 | auto_deref => 1, | ||||||||||
| 26 | lazy => 1, | ||||||||||
| 27 | 1 | ▶ | default => sub { $_[0]->_compute_changes }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ); | |||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 31 | 1 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | croak "The instances must be of the same class" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | unless $self->from->meta->name eq $self->to->meta->name; | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub _compute_changes { | ||||||||
|
Subroutine
_compute_changes: called CC=3 SCAR=11.0
| |||||||||||
| 38 | 1 | my $self = shift; | |||||||||
| 39 | |||||||||||
| 40 | 1 | my %changes; | |||||||||
| 41 | |||||||||||
| 42 | 1 | my ( $from, $to ) = ( $self->from, $self->to ); | |||||||||
| 43 | |||||||||||
| 44 | 1 | my @attrs = $from->meta->get_all_attributes; | |||||||||
| 45 | |||||||||||
| 46 | 1 | foreach my $attr ( grep { $_->name !~ /^(?:prev|state|transition)$/ } @attrs ) { | |||||||||
| 47 | 3 | my $res = $self->_compare_values( | |||||||||
| 48 | $attr, | ||||||||||
| 49 | $attr->get_value( $from ), | ||||||||||
| 50 | $attr->get_value( $to ), | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 3 | ▶ | $changes{$attr->name} = $res if $res; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1 | return \%changes; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 3 | ▶ | sub _compare_values { | ||||||||
|
Subroutine
_compare_values: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 3 | my ( $self, $attr, $from, $to ) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 3 | ▶ | unless ( Data::Compare::Compare( $from, $to ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | return { from => $from, to => $to }; | |||||||||
| 64 | } else { | ||||||||||
| 65 | 2 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | __PACKAGE__; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||