RuledCluster.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 71.0% 27 / 38 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 17 / 17 Podpod 88.8% 8 / 9 totaltotal 84.3% 129 / 153 CC 25 SCAR 16.7
CC
25
Cov
82%
resolve_node_keys
11.3
_delegate
8.3
resolve
4.2
CRAP 5.3
« RuledCluster/Strategy/List.pm ↑ Summary RuledCluster/Strategy.pm »
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
BranchTrueFalseTotal
@_ == 1 ? : 0 6 6
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
BranchTrueFalseTotal
$pkg =~ s/^\+// ? : 0 59 59
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
BranchTrueFalseTotal
if $config 8 1 9
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
BranchTrueFalseTotal
unless $self->{"config"} 0 65 65
36  
37 65 ▶ if ($self->is_cluster($cluster_or_node)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_cluster($cluster_or_node)) { } 36 29 65
elsif ($self->is_node($cluster_or_node)) { } 29 0 29
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
BranchTrueFalseTotal
if (is_hash_ref($args)) { } 0 23 23
53 0 $args = {%$args}; # shallow copy
54 0 ▶ $args->{strategy} ||= 'Key';
Condition0%0/2
$args->{"strategy"} ||= "Key"
l!l
00
MC/DC0%0/1
$args->{"strategy"} ||= "Key"
$args->{"strategy"}
Truth table0%0/2
A
A= $args->{"strategy"}
Aresult
11
00
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
BranchTrueFalseTotal
if $self->is_node($cluster_or_node) 25 7 32
64 7 ▶ if ($self->is_cluster($cluster_or_node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_cluster($cluster_or_node)) 7 0 7
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} ||= []
l!l
610
MC/DC100%1/1
$node_keys{$node} ||= []
$node_keys{$node}
Truth table100%2/2
A
A= $node_keys{$node}
Aresult
11
00
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
BranchTrueFalseTotal
wantarray ? : 16 7 23
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
BranchTrueFalseTotal
unless $self->{"config"} 0 59 59
85  
86 59 ▶ if ( is_hash_ref($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($args)) 0 59 59
87 0 ▶ Carp::croak("args has not 'strategy' field") unless $args->{strategy};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"strategy"} 0 0 0
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
BranchTrueFalseTotal
if (is_array_ref($cluster_info)) { } 18 41 59
elsif (is_hash_ref($cluster_info)) { } 41 0 41
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
BranchTrueFalseTotal
is_array_ref($cluster_info) ? : 22 7 29
134 29 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 27 2 29
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
BranchTrueFalseTotal
exists $self->{'config'}{'clusters'}{$cluster} ? : 61 62 123
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
BranchTrueFalseTotal
exists $self->{'config'}{'node'}{$node} ? : 58 7 65
145 }
146  
147 7 1;
148 __END__
« RuledCluster/Strategy/List.pm ↑ Summary RuledCluster/Strategy.pm »