| 1 | package Data::RuledCluster; | ||||||||||||||
| 2 | 7 | ▶ | use 5.008_001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 7 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 7 | ▶ | use Class::Load (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 7 | ▶ | use Data::Util qw(is_array_ref is_hash_ref); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 7 | our $VERSION = '0.07'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 6 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 12 | 6 | my $class = shift; | |||||||||||||
| 13 | 0 | ▶ | my %args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 14 | 6 | bless \%args, $class; | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 59 | ▶ | sub resolver { | ||||||||||||
|
Subroutine
resolver: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 18 | 59 | my ($self, $strategy) = @_; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 59 | my $pkg = $strategy; | |||||||||||||
| 21 | 59 | ▶ | $pkg = $pkg =~ s/^\+// ? $pkg : "Data::RuledCluster::Strategy::$pkg"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 59 | Class::Load::load_class($pkg); | |||||||||||||
| 23 | 59 | $pkg; | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 9 | ▶ | sub config { | ||||||||||||
|
Subroutine
config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 27 | 9 | my ($self, $config) = @_; | |||||||||||||
| 28 | 9 | ▶ | $self->{config} = $config if $config; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 9 | $self->{config}; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 65 | ▶ | sub resolve { | ||||||||||||
|
Subroutine
resolve: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||
| 33 | 65 | my ($self, $cluster_or_node, $args, $options) = @_; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 65 | ▶ | Carp::croak("missing mandatory config.") unless $self->{config}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 65 | ▶ | if ($self->is_cluster($cluster_or_node)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 38 | 36 | my ($resolved_node, @keys) = $self->_delegate(resolve => $cluster_or_node, $args, $options); | |||||||||||||
| 39 | 34 | return $self->resolve($resolved_node, \@keys); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | elsif ($self->is_node($cluster_or_node)) { | ||||||||||||||
| 42 | 29 | return $self->_make_node_hashref($cluster_or_node); | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | Carp::croak("$cluster_or_node is not defined."); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 23 | ▶ | sub resolve_node_keys { | ||||||||||||
|
Subroutine
resolve_node_keys: called CC=10 SCAR=24.3
Pod
documented
| |||||||||||||||
| 49 | 23 | my ($self, $cluster_or_node, $keys, $args, $options) = @_; | |||||||||||||
| 50 | 23 | my $orig_args = $args; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 23 | ▶ | if ( is_hash_ref $args ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | $args = {%$args}; # shallow copy | |||||||||||||
| 54 | 0 | ▶ | $args->{strategy} ||= 'Key'; | ||||||||||||
|
Condition0%0/2
$args->{"strategy"} ||= "Key"
MC/DC0%0/1
$args->{"strategy"} ||= "Key"
$args->{"strategy"}
Truth table0%0/2
A
A= $args->{"strategy"}
| |||||||||||||||
| 55 | 0 | $args->{key} = $keys; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | else { | ||||||||||||||
| 58 | 23 | $args = $keys; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 23 | my %node_keys = $self->_delegate(resolve_node_keys => $cluster_or_node, $args, $options); | |||||||||||||
| 62 | 23 | for my $cluster_or_node (keys %node_keys) { | |||||||||||||
| 63 | 32 | ▶ | next if $self->is_node($cluster_or_node); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 7 | ▶ | if ($self->is_cluster($cluster_or_node)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 7 | my $cluster = $cluster_or_node; | |||||||||||||
| 66 | 7 | my @key_info = @{ delete $node_keys{$cluster_or_node} }; | |||||||||||||
| 67 | 7 | for my $key_info (@key_info) { | |||||||||||||
| 68 | 16 | my %child_node_keys = $self->resolve_node_keys($cluster, $key_info->{next}, $orig_args, $options); | |||||||||||||
| 69 | 16 | for my $node (keys %child_node_keys) { | |||||||||||||
| 70 | 16 | ▶ | push @{ $node_keys{$node} ||= [] } => $key_info->{root}; | ||||||||||||
|
Condition100%2/2
$node_keys{$node} ||= []
MC/DC100%1/1
$node_keys{$node} ||= []
$node_keys{$node}
Truth table100%2/2
A
A= $node_keys{$node}
| |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | 7 | next; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | Carp::croak("$cluster_or_node is not defined."); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 23 | ▶ | return wantarray ? %node_keys : \%node_keys; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 59 | ▶ | sub _delegate { | ||||||||||||
|
Subroutine
_delegate: called CC=6 SCAR=21.2
| |||||||||||||||
| 82 | 59 | my ($self, $method, $cluster, $args, $options) = @_; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 59 | ▶ | Carp::croak("missing mandatory config.") unless $self->{config}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | |||||||||||||||
| 86 | 59 | ▶ | if ( is_hash_ref($args) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | ▶ | Carp::croak("args has not 'strategy' field") unless $args->{strategy}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 0 | return $self->resolver($args->{strategy})->$method( | |||||||||||||
| 89 | $self, | ||||||||||||||
| 90 | $cluster, | ||||||||||||||
| 91 | $args, | ||||||||||||||
| 92 | $options, | ||||||||||||||
| 93 | ); | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 59 | my $cluster_info = $self->cluster_info($cluster); | |||||||||||||
| 97 | 59 | ▶ | if (is_array_ref($cluster_info)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 98 | 18 | return $self->resolver('Key')->$method( | |||||||||||||
| 99 | $self, | ||||||||||||||
| 100 | $cluster, | ||||||||||||||
| 101 | +{ key => $args, }, | ||||||||||||||
| 102 | $options, | ||||||||||||||
| 103 | ); | ||||||||||||||
| 104 | } | ||||||||||||||
| 105 | elsif (is_hash_ref($cluster_info)) { | ||||||||||||||
| 106 | 41 | return $self->resolver($cluster_info->{strategy})->$method( | |||||||||||||
| 107 | $self, | ||||||||||||||
| 108 | $cluster, | ||||||||||||||
| 109 | +{ %$cluster_info, key => $args, }, | ||||||||||||||
| 110 | $options, | ||||||||||||||
| 111 | ); | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | Carp::croak('$cluster_info is invalid.'); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 29 | ▶ | sub _make_node_hashref { | ||||||||||||
|
Subroutine
_make_node_hashref: called CC=1 SCAR=0.0
| |||||||||||||||
| 118 | 29 | my ($self, $node) = @_; | |||||||||||||
| 119 | return { | ||||||||||||||
| 120 | node => $node, | ||||||||||||||
| 121 | 29 | node_info => $self->{config}->{node}->{$node}, | |||||||||||||
| 122 | }; | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 88 | ▶ | sub cluster_info { | ||||||||||||
|
Subroutine
cluster_info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 126 | 88 | my ($self, $cluster) = @_; | |||||||||||||
| 127 | 88 | $self->{config}->{clusters}->{$cluster}; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 29 | ▶ | sub clusters { | ||||||||||||
|
Subroutine
clusters: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 131 | 29 | my ($self, $cluster) = @_; | |||||||||||||
| 132 | 29 | my $cluster_info = $self->cluster_info($cluster); | |||||||||||||
| 133 | 29 | ▶ | my @nodes = is_array_ref($cluster_info) ? @$cluster_info : @{ $cluster_info->{nodes} }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 29 | ▶ | wantarray ? @nodes : \@nodes; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 123 | ▶ | sub is_cluster { | ||||||||||||
|
Subroutine
is_cluster: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 138 | 123 | my ($self, $cluster) = @_; | |||||||||||||
| 139 | 123 | ▶ | exists $self->{config}->{clusters}->{$cluster} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 65 | ▶ | sub is_node { | ||||||||||||
|
Subroutine
is_node: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 143 | 65 | my ($self, $node) = @_; | |||||||||||||
| 144 | 65 | ▶ | exists $self->{config}->{node}->{$node} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 7 | 1; | |||||||||||||
| 148 | __END__ | ||||||||||||||