CCfnX/DSL/Inheritance.pm

filter: Statementstmt 90.0% 54 / 60 Branchbran 79.1% 19 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 1 totaltotal 82.3% 84 / 102 CC 14 SCAR 11.9
CC
14
Cov
87%
_get_inheritance_dsl_behavior
5.1
resolve_resource_inheritance_dsl
5.0
_apply_inheritance_dsl
5.0
CRAP 3.3
« Imager/TabularDisplay.pm ↑ Summary CCfnX/UserData.pm »
1 15 package CCfnX::DSL::Inheritance {
2 15 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶ use Hash::Merge qw/merge _merge_hashes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 Hash::Merge::specify_behavior({
6  
7 # Left side always has precedence in the case of scalars
8 # Avoid merging different types
9 'SCALAR' => {
10 1 ▶ 'SCALAR' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 0 ▶ 'ARRAY' => sub { die "Can't merge a scalar with an an array. Use replace behavior instead" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 ▶ 'HASH' => sub { die "Can't merge a scalar with a hash. Use replace behavior instead" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 },
14 'ARRAY' => {
15 0 ▶ 'SCALAR' => sub { die "Can't merge an array with a scalar" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 5 ▶ 'ARRAY' => sub { [ @{ $_[0] }, @{ $_[1] } ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 0 ▶ 'HASH' => sub { die "Can't merge an array with a hash" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 },
19 'HASH' => {
20 0 ▶ 'SCALAR' => sub { die "Can't merge a hash with a scalar" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 ▶ 'ARRAY' => sub { die "Can't merge a hash with an array" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 1 ▶ 'HASH' => sub { _merge_hashes( $_[0], $_[1] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 },
24 15 });
25  
26 15 Moose::Exporter->setup_import_methods(
27 as_is => [ qw/resolve_resource_inheritance_dsl/ ]
28 );
29  
30 83 ▶ sub resolve_resource_inheritance_dsl {
Subroutine resolve_resource_inheritance_dsl: called CC=5 SCAR=16.1
Pod undocumented
31 # receives a hash with keys 'resource', 'properties', 'extra'
32 83 my ($params) = @_;
33  
34 83 my $meta = $params->{meta};
35 83 my ($inherited, $attr_name) = _is_inherited($params->{name});
36  
37 83 ▶ if (not $inherited) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $inherited) { } 71 12 83
38 die "Redeclared resource/output/condition/parameter/mapping $params->{name}"
39 71 ▶ if ($meta->find_attribute_by_name($params->{name}));
Branch: 1 branch
BranchTrueFalseTotal
if $meta->find_attribute_by_name($params->{"name"}) 1 70 71
40  
41 75 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 return Moose::Util::TypeConstraints::find_type_constraint('Cfn::Resource')->coerce({
43 Type => $params->{resource},
44 Properties => $params->{properties},
45 75 %{$params->{extra}},
46 });
47 }
48 70 }
49  
50 else {
51  
52 12 my $attr = $meta->find_attribute_by_name($attr_name);
53 12 ▶ die "Couldn't find any resource with name $attr_name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 12 12
54 if (not defined $attr);
55 die "Attribute $attr_name exist but is not of type " . $params->{attr_family}
56 12 ▶ if (not $attr->does($params->{attr_family}));
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->does($params->{"attr_family"}) 0 12 12
57  
58 # Execute the default attribute value coderef to get the superclass resource
59 12 my $object = &{$attr->default};
60  
61 12 $object = _apply_inheritance_dsl($object, $params->{extra});
62 12 $object->Properties(_apply_inheritance_dsl($object->Properties, $params->{properties}));
63  
64 12 ▶ return sub { return $object};
Subroutine __ANON__: called CC=1 SCAR=0.0
65 }
66  
67 };
68  
69 # This method applies DSL inheritance for each key defined in the given passed objects
70 24 ▶ sub _apply_inheritance_dsl {
Subroutine _apply_inheritance_dsl: called CC=5 SCAR=16.1
71 24 my ($inherited_object, $hashref_with_dsl) = @_;
72  
73 24 foreach my $attr_with_dsl (keys %{$hashref_with_dsl}) {
74  
75 18 my ($behavior, $attr_name) = _get_inheritance_dsl_behavior($attr_with_dsl);
76  
77 18 ▶ if ($behavior eq "REPLACE") {
Branch: 3 branches
BranchTrueFalseTotal
if ($behavior eq 'REPLACE') { } 10 8 18
elsif ($behavior eq 'MERGE') { } 6 2 8
elsif ($behavior eq 'DELETE') { } 2 0 2
78 10 my $attr_new_value = $hashref_with_dsl->{$attr_with_dsl};
79 10 $inherited_object->$attr_name($attr_new_value);
80 }
81  
82 elsif ($behavior eq "MERGE") {
83  
84 6 my $attr_inherited_value = $inherited_object->$attr_name;
85 6 my $attr_type_constraint = $inherited_object->meta->find_attribute_by_name($attr_name)->type_constraint;
86 6 my $attr_new_value = Moose::Util::TypeConstraints::find_type_constraint($attr_type_constraint)->coerce( $hashref_with_dsl->{$attr_with_dsl} );
87  
88 # Merge the new value with the old one
89 6 $attr_inherited_value->Value(merge($attr_new_value->Value,$attr_inherited_value->Value));
90 }
91  
92 elsif ($behavior eq "DELETE") {
93 2 delete $inherited_object->{$attr_name};
94 }
95 }
96  
97 24 return $inherited_object;
98 };
99  
100 83 ▶ sub _is_inherited {
Subroutine _is_inherited: called CC=2 SCAR=6.9
101 83 my $name = shift;
102 83 ▶ if ( $name =~ /\+(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\+(.*)/) { } 12 71 83
103 12 return (1, substr($name,1));
104 } else {
105 71 return (0, $name);
106 }
107 };
108  
109 18 ▶ sub _get_inheritance_dsl_behavior {
Subroutine _get_inheritance_dsl_behavior: called CC=5 SCAR=16.3
110 18 my $name = shift;
111 18 ▶ die "Unrecognized resource property inheritance DSL: $name. Allowed values are: '+', '-', '~'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\+(.*)|\~(.*)|-(.*)/ 0 18 18
112 if ( $name !~ /\+(.*)|\~(.*)|-(.*)/ );
113  
114 18 ▶ return ("REPLACE", substr($name,1)) if ( $name =~ /\+(.*)/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\+(.*)/ 10 8 18
115 8 ▶ return ("DELETE", substr($name,1)) if ( $name =~ /\-(.*)/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\-(.*)/ 2 6 8
116 6 ▶ return ("MERGE", substr($name,1)) if ( $name =~ /\~(.*)/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\~(.*)/ 6 0 6
117 }
118  
119 };
120  
121 15 1;
« Imager/TabularDisplay.pm ↑ Summary CCfnX/UserData.pm »