| 1 | package autobox::Lookup; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: easy data lookup | ||||||||||||||||||||||||||||
| 4 | 5 | { our $VERSION = '0.002'; } | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use base qw(autobox); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 9 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | $autobox::Lookup::Subs::sep = shift() || '.'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || '.'
MC/DC0%0/1
shift() || '.'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | $class->SUPER::import( | |||||||||||||||||||||||||||
| 14 | HASH => 'autobox::Lookup::Subs', | ||||||||||||||||||||||||||||
| 15 | ARRAY => 'autobox::Lookup::Subs', | ||||||||||||||||||||||||||||
| 16 | SCALAR => 'autobox::Lookup::Subs', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package autobox::Lookup::Subs; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use List::Util qw/reduce/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | *_lookup = *_recursive_lookup; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=12.3
| |||||||||||||||||||||||||||||
| 28 | 5 | eval { require Mojo::Collection }; | |||||||||||||||||||||||||||
| 29 | 5 | ▶ | if (!$@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | Mojo::Collection->import(); | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | *collection = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 32 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | if (ref($data) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | 0 | Mojo::Collection->new($data->@*) | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | elsif (ref($data) eq 'HASH') { | ||||||||||||||||||||||||||||
| 37 | 0 | Mojo::Collection->new(values $data->%*) | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | }; | |||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | *collection = sub { die "collection is not implemented unless Mojo::Collection is installed\n" } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 5 | } | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _recursive_lookup { | ||||||||||||||||||||||||||
|
Subroutine
_recursive_lookup: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($data, $key) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return \undef unless defined $data && ref($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $data and ref $data
MC/DC0%0/2
defined $data and ref $data
defined $data
ref $data
Truth table0%0/3
A and B
A= defined $dataB= ref $data
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return \$data unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my ($first, @rest) = split /\./, $key; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($key =~ /\,/) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 54 | 0 | return do { my $r = [ map { ${_lookup($data, $_)} } split /\,/, $key ]; \$r } | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | ▶ | elsif (ref($data) eq 'ARRAY' && $first =~ /^\d+$/) { | |||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "ARRAY" and $first =~ /^\d+$/
MC/DC0%0/2
ref $data eq "ARRAY" and $first =~ /^\d+$/
ref $data eq "ARRAY"
$first =~ /^\d+$/
Truth table0%0/3
A and B
A= ref $data eq "ARRAY"B= $first =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return @rest ? _lookup($data->[$first], join('.', @rest)) : \($data->[$first]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | ▶ | elsif (ref($data) eq 'ARRAY' && $first eq '[]') { | |||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "ARRAY" and $first eq "[]"
MC/DC0%0/2
ref $data eq "ARRAY" and $first eq "[]"
ref $data eq "ARRAY"
$first eq "[]"
Truth table0%0/3
A and B
A= ref $data eq "ARRAY"B= $first eq "[]"
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | return @rest ? do { my $t = [ map { ${_lookup($_, join('.', @rest))} } @$data ]; \$t } : \$data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | ▶ | elsif (ref($data) eq 'HASH' && $first eq '[]') { | |||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "HASH" and $first eq "[]"
MC/DC0%0/2
ref $data eq "HASH" and $first eq "[]"
ref $data eq "HASH"
$first eq "[]"
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= $first eq "[]"
| |||||||||||||||||||||||||||||
| 63 | return @rest ? | ||||||||||||||||||||||||||||
| 64 | 0 | do { my $t = [ map { ${_lookup($data->{$_}, join('.', @rest))} } sort keys %$data ]; \$t } : | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | do { my $t = [ map { $data->{$_} } sort keys %$data ]; \$t; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | ▶ | elsif (ref($data) eq 'HASH' && exists $data->{$first}) { | |||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "HASH" and exists $data->{$first}
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{$first}
ref $data eq "HASH"
exists $data->{$first}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= exists $data->{$first}
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return @rest ? _lookup($data->{$first}, join('.', @rest)) : \($data->{$first}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | return \undef; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub lookup { shift()->get(@_) } | ||||||||||||||||||||||||||
|
Subroutine
lookup: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 0 | my $h = shift; | |||||||||||||||||||||||||||
| 77 | 0 | my $k = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | my $l = _lookup($h, $k); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return $$l | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 0 | my ($h, $k, $v) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $l = _lookup($h, $k); | |||||||||||||||||||||||||||
| 88 | 0 | $$l = $v; | |||||||||||||||||||||||||||
| 89 | 0 | return $h | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (ref $_[0] eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | 0 | return [ values $_[0]->%* ] | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | elsif (ref $_[0] eq "ARRAY") { | ||||||||||||||||||||||||||||
| 97 | 0 | return $_[0] | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub keys { | ||||||||||||||||||||||||||
|
Subroutine
keys: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (ref $_[0] eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 103 | 0 | return [ keys $_[0]->%* ] | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | elsif (ref $_[0] eq "ARRAY") { | ||||||||||||||||||||||||||||
| 106 | 0 | return [ 0..$#{$_[0]} ] | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 5 | 1; | |||||||||||||||||||||||||||