| 1 | package KiokuDB::Role::WithDigest; | ||||||||||||||||||
| 2 | 3 | ▶ | use Moose::Role; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 3 | ▶ | use Digest::SHA qw(sha1_hex); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 3 | ▶ | use MooseX::Clone::Meta::Attribute::Trait::NoClone; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 3 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 3 | has digest => ( | |||||||||||||||||
| 13 | traits => [qw(NoClone)], | ||||||||||||||||||
| 14 | isa => "Str", | ||||||||||||||||||
| 15 | is => "ro", | ||||||||||||||||||
| 16 | lazy_build => 1, | ||||||||||||||||||
| 17 | ); | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 3 | requires 'digest_parts'; | |||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | sub _build_digest { | ||||||||||||||||||
| 22 | 120 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
_build_digest: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 23 | 120 | $self->_compute_digest( $self->_build_digest_strings ); | |||||||||||||||||
| 24 | } | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | sub _compute_digest { | ||||||||||||||||||
| 27 | 120 | ▶ | my ( $self, @strings ) = @_; | ||||||||||||||||
|
Subroutine
_compute_digest: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | 3 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 30 | 120 | sha1_hex(join ":", ref($self), @strings); | |||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | sub _build_digest_strings { | ||||||||||||||||||
| 34 | 120 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
_build_digest_strings: called CC=3 SCAR=11.2
| |||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 120 | my @parts = $self->digest_parts; | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 120 | my @strings; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 120 | foreach my $part ( $self->digest_parts ) { | |||||||||||||||||
| 41 | 240 | ▶ | if ( ref $part ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 0 | push @strings, $self->_extract_digest_input_strings($part); | |||||||||||||||||
| 43 | } else { | ||||||||||||||||||
| 44 | 240 | push @strings, $part; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 120 | return @strings; | |||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | sub _extract_digest_input_strings { | ||||||||||||||||||
| 52 | 0 | ▶ | my ( $self, $part ) = @_; | ||||||||||||||||
|
Subroutine
_extract_digest_input_strings: not called CC=7 SCAR=37.2
| |||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 0 | ▶ | return $part unless ref $part; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 3 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 0 | ▶ | if ( blessed($part) ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 59 | 0 | ▶ | if ( $part->can("kiokudb_object_id") ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 60 | 0 | return $part->kiokudb_object_id; | |||||||||||||||||
| 61 | } elsif ( $part->can("digest") ) { | ||||||||||||||||||
| 62 | 0 | return $part->digest; | |||||||||||||||||
| 63 | } else { | ||||||||||||||||||
| 64 | 0 | croak "Can't digest $part (no digest or ID method)"; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | } elsif ( ref $part eq 'ARRAY' ) { | ||||||||||||||||||
| 67 | 0 | return join("", '[', join(",", map { $self->_extract_digest_input_strings($_) } @$part), ']'); | |||||||||||||||||
| 68 | } elsif ( ref $part eq 'HASH' ) { | ||||||||||||||||||
| 69 | 0 | return join("", '{', join(",", map { $_, ":", $self->_extract_digest_input_strings($part->{$_}) } sort keys %$part), '}'); | |||||||||||||||||
| 70 | } else { | ||||||||||||||||||
| 71 | 0 | croak "Can't digest $part (not a simple ref type)"; | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 3 | __PACKAGE__ | |||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||