| 1 | package Catmandu::Fix::Bind::hashmap; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.2025'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Catmandu::Util qw(:is); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | with 'Catmandu::Fix::Bind', 'Catmandu::Fix::Bind::Group'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | has exporter => (fix_opt => 1); | |||||||||||||||||||||||||||
| 15 | 1 | has store => (fix_opt => 1); | |||||||||||||||||||||||||||
| 16 | 11 | ▶ | has uniq => (fix_opt => 1, default => sub {0}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | has count => (fix_opt => 1); | |||||||||||||||||||||||||||
| 18 | 1 | has join => (fix_opt => 1); | |||||||||||||||||||||||||||
| 19 | 1 | has extra_args => (fix_opt => 'collect'); | |||||||||||||||||||||||||||
| 20 | 1 | has hash => (is => 'lazy'); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 12 | ▶ | sub _build_hash { | ||||||||||||||||||||||||||
|
Subroutine
_build_hash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 12 | +{}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | sub add_to_hash { | ||||||||||||||||||||||||||
|
Subroutine
add_to_hash: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 9 | my ($self, $key, $val) = @_; | |||||||||||||||||||||||||||
| 28 | 9 | ▶ | if ($self->count) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | 3 | $self->hash->{$key} += 1; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | elsif ($self->uniq) { | ||||||||||||||||||||||||||||
| 32 | 3 | $self->hash->{$key}->{$val} = 1; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||||||||
| 35 | 3 | push @{$self->hash->{$key}}, $val; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 18 | ▶ | sub bind { | ||||||||||||||||||||||||||
|
Subroutine
bind: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 18 | my ($self, $data, $code) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | $data = $code->($data); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 18 | my $key = $data->{key}; | |||||||||||||||||||||||||||
| 45 | 18 | my $value = $data->{value}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 18 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | if (is_string($key)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 9 | $self->add_to_hash($key, $value); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | elsif (is_array_ref($key)) { | ||||||||||||||||||||||||||||
| 52 | 0 | for (@$key) { | |||||||||||||||||||||||||||
| 53 | 0 | $self->add_to_hash($_, $value); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||
| 57 | 0 | warn "$key is not a string or array for $value"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 18 | $data; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 12 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 65 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 66 | 12 | my $h = $self->hash; | |||||||||||||||||||||||||||
| 67 | 12 | my $e; | |||||||||||||||||||||||||||
| 68 | 12 | ▶ | my $args = $self->extra_args // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->extra_args // {}
MC/DC0%0/1
$self->extra_args // {}
$self->extra_args
Truth table50%1/2
A
A= $self->extra_args
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | if ($self->store) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 71 | 0 | $e = Catmandu->store($self->store, %$args); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | elsif ($self->exporter) { | ||||||||||||||||||||||||||||
| 74 | 3 | $e = Catmandu->exporter($self->exporter, %$args); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 9 | $e = Catmandu->exporter('JSON', line_delimited => 1); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 12 | ▶ | my $sorter = $self->count ? sub {$h->{$b} <=> $h->{$a}} : sub {$a cmp $b}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 12 | my $id = 0; | |||||||||||||||||||||||||||
| 83 | 12 | for (sort $sorter keys %$h) { | |||||||||||||||||||||||||||
| 84 | 6 | my $v; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 6 | ▶ | if ($self->count) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 2 | $v = $h->{$_}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | elsif ($self->uniq) { | ||||||||||||||||||||||||||||
| 90 | 2 | $v = [sort keys %{$h->{$_}}]; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 2 | $v = $h->{$_}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 6 | ▶ | if (is_array_ref($v) && $self->join) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_array_ref($v) and $self->join
MC/DC100%2/2
is_array_ref($v) and $self->join
is_array_ref($v)
$self->join
Truth table100%3/3
A and B
A= is_array_ref($v)B= $self->join
| |||||||||||||||||||||||||||||
| 97 | 2 | $v = join $self->join, @$v; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 6 | $e->add({_id => $_, value => $v}); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 12 | $e->commit; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | 1; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||