blib/lib/Net/Amazon/DynamoDB/Table.pm

filter: Statementstmt 19.5% 19 / 97 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 20.0% 3 / 15 Podpod 55.5% 5 / 9 totaltotal 12.8% 27 / 211 CC 32 SCAR 36.5
CC
32
Cov
12%
get
72.0
delete
72.0
inflate_attribute_value
56.0
CRAP 38.4
↑ Summary Build »
1 package Net::Amazon::DynamoDB::Table;
2 1 ▶use Net::Amazon::DynamoDB::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp qw/cluck confess carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION="0.04";
7  
8 1 has table => (is => 'rw', required => 1);
9 1 has hash_key => (is => 'rw', required => 1);
10 1 has range_key => (is => 'rw', required => 0);
11 1 has dynamodb => (is => 'lazy');
12 1 has region => (is => 'rw', required => 1);
13 1 has access_key => (is => 'rw', lazy => 1, builder => 1);
14 1 has secret_key => (is => 'rw', lazy => 1, builder => 1);
15 1 has timeout => (is => 'rw', default => 5);
16  
17 0 ▶sub _build_access_key { $ENV{AWS_ACCESS_KEY} }
Subroutine _build_access_key: not called CC=1 SCAR=6.9
18 0 ▶sub _build_secret_key { $ENV{AWS_SECRET_KEY} }
Subroutine _build_secret_key: not called CC=1 SCAR=6.9
19  
20 sub _build_dynamodb {
21 0 ▶ my $self = shift;
Subroutine _build_dynamodb: not called CC=1 SCAR=6.9
22 0 Net::Amazon::DynamoDB::Lite->new(
23 region => $self->region,
24 access_key => $self->access_key,
25 secret_key => $self->secret_key,
26 connection_timeout => $self->timeout,
27 );
28 }
29  
30 sub get {
31 0 ▶ my ($self, %args) = @_;
Subroutine get: not called CC=8 SCAR=42.8
Pod documented
32 0 my $hash_key = $self->hash_key;
33 0 my $range_key = $self->range_key;
34 0 my $primary_key = {};
35  
36 0 ▶ die "Key or $hash_key param required" unless $args{Key} || $args{$hash_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"Key"} or $args{$hash_key} 0 0 0
Condition0%0/3
$args{"Key"} or $args{$hash_key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"Key"} or $args{$hash_key}
$args{"Key"} $args{$hash_key}
Truth table0%0/3
A or B
A= $args{"Key"}B= $args{$hash_key}
ABresult
1X1
011
000
37  
38 0 ▶ if ($args{$hash_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$hash_key}) 0 0 0
39 0 my $value = delete $args{$hash_key};
40 0 $primary_key->{$hash_key} = $self->inflate_attribute_value($value);
41 }
42 0 ▶ if ($range_key && $args{$range_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_key and $args{$range_key}) 0 0 0
Condition0%0/3
$range_key and $args{$range_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$range_key and $args{$range_key}
$range_key $args{$range_key}
Truth table0%0/3
A and B
A= $range_keyB= $args{$range_key}
ABresult
0X0
100
111
43 0 my $value = delete $args{$range_key};
44 0 $primary_key->{$range_key} = $self->inflate_attribute_value($value);
45 }
46  
47 0 ▶ $args{Key} ||= $primary_key;
Condition0%0/3
$args{"Key"} ||= $primary_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"Key"} ||= $primary_key
$args{"Key"} $primary_key
Truth table0%0/3
A ||= B
A= $args{"Key"}B= $primary_key
ABresult
1X1
011
000
48 0 ▶ $args{TableName} ||= $self->table;
Condition0%0/3
$args{"TableName"} ||= $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"TableName"} ||= $self->table
$args{"TableName"} $self->table
Truth table0%0/3
A ||= B
A= $args{"TableName"}B= $self->table
ABresult
1X1
011
000
49  
50 0 return $self->dynamodb->get_item(\%args);
51 }
52  
53 sub put {
54 0 ▶ my ($self, %args) = @_;
Subroutine put: not called CC=3 SCAR=24.8
Pod documented
55  
56 0 ▶ die "Item param required" unless $args{Item};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"Item"} 0 0 0
57  
58 0 ▶ $args{TableName} ||= $self->table;
Condition0%0/3
$args{"TableName"} ||= $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"TableName"} ||= $self->table
$args{"TableName"} $self->table
Truth table0%0/3
A ||= B
A= $args{"TableName"}B= $self->table
ABresult
1X1
011
000
59 0 $args{Item} = $self->inflate_item($args{Item});
60  
61 0 return $self->dynamodb->put_item(\%args);
62 }
63  
64 sub delete {
65 0 ▶ my ($self, %args) = @_;
Subroutine delete: not called CC=8 SCAR=42.8
Pod documented
66 0 my $hash_key = $self->hash_key;
67 0 my $range_key = $self->range_key;
68 0 my $primary_key = {};
69  
70 0 ▶ die "Key or $hash_key param required" unless $args{Key} || $args{$hash_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"Key"} or $args{$hash_key} 0 0 0
Condition0%0/3
$args{"Key"} or $args{$hash_key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"Key"} or $args{$hash_key}
$args{"Key"} $args{$hash_key}
Truth table0%0/3
A or B
A= $args{"Key"}B= $args{$hash_key}
ABresult
1X1
011
000
71  
72 0 ▶ if ($args{$hash_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$hash_key}) 0 0 0
73 0 my $value = delete $args{$hash_key};
74 0 $primary_key->{$hash_key} = $self->inflate_attribute_value($value);
75 }
76 0 ▶ if ($range_key && $args{$range_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_key and $args{$range_key}) 0 0 0
Condition0%0/3
$range_key and $args{$range_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$range_key and $args{$range_key}
$range_key $args{$range_key}
Truth table0%0/3
A and B
A= $range_keyB= $args{$range_key}
ABresult
0X0
100
111
77 0 my $value = delete $args{$range_key};
78 0 $primary_key->{$range_key} = $self->inflate_attribute_value($value);
79 }
80  
81 0 ▶ $args{Key} ||= $primary_key;
Condition0%0/3
$args{"Key"} ||= $primary_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"Key"} ||= $primary_key
$args{"Key"} $primary_key
Truth table0%0/3
A ||= B
A= $args{"Key"}B= $primary_key
ABresult
1X1
011
000
82 0 ▶ $args{TableName} ||= $self->table;
Condition0%0/3
$args{"TableName"} ||= $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"TableName"} ||= $self->table
$args{"TableName"} $self->table
Truth table0%0/3
A ||= B
A= $args{"TableName"}B= $self->table
ABresult
1X1
011
000
83  
84 0 return $self->dynamodb->delete_item(\%args);
85 }
86  
87 sub inflate_item {
88 0 ▶ my ($self, $item) = @_;
Subroutine inflate_item: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my %new;
90  
91 0 for my $attr (keys %$item) {
92 0 my $val = $item->{$attr};
93 0 $new{$attr} = $self->inflate_attribute_value($val);
94 }
95  
96 0 return \%new;
97 }
98  
99 sub inflate_hash_key {
100 0 ▶ my ($self, $thing) = @_;
Subroutine inflate_hash_key: not called CC=3 SCAR=24.8
Pod undocumented
101 0 my %new;
102  
103 0 ▶ if (ref($thing) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq 'HASH') { } 0 0 0
104 0 my %new;
105 0 for my $key (keys %$thing) {
106 0 $new{$key} = $self->inflate_attribute_value($thing->{$key});
107 }
108 0 return \%new;
109 }
110 else {
111 0 return $self->inflate_attribute_value($thing);
112 }
113 }
114  
115 sub inflate_attribute_value {
116 0 ▶ my ($self, $thing) = @_;
Subroutine inflate_attribute_value: not called CC=7 SCAR=40.3
Pod undocumented
117  
118 0 ▶ if (ref($thing) eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $thing eq 'HASH') { } 0 0 0
elsif (ref $thing eq 'ARRAY') { } 0 0 0
elsif (ref $thing eq 'SCALAR') { } 0 0 0
elsif ($self->isa_number($thing)) { } 0 0 0
119 0 my %vals;
120 0 for my $key (keys %$thing) {
121 0 $vals{$key} = $self->inflate_attribute_value($thing->{$key});
122 }
123 0 return { M => \%vals };
124 }
125 elsif (ref($thing) eq 'ARRAY') {
126 0 my @vals;
127 0 push @vals, $self->inflate_attribute_value($_) for @$thing;
128 0 return { L => [@vals] };
129 }
130 elsif (ref($thing) eq 'SCALAR') {
131 0 return { B => MIME::Base64::encode_base64($$thing, '') };
132 }
133 elsif ($self->isa_number($thing)) {
134 0 return { N => "$thing" };
135 }
136 else {
137 0 return { S => $thing };
138 }
139 }
140  
141 sub isa_number {
142 0 ▶ my ($self, $thing) = @_;
Subroutine isa_number: not called CC=4 SCAR=30.0
Pod undocumented
143 0 ▶ return 1 if B::svref_2object(\$thing)->FLAGS & (B::SVp_IOK | B::SVp_NOK)
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object(\$thing)->FLAGS & 12288 and 0 + $thing eq $thing and $thing * 0 == 0 0 0 0
Condition0%0/6
B::svref_2object(\$thing)->FLAGS & 12288 and 0 + $thing eq $thing and $thing * 0 == 0
!ll&&!rl&&r
000
B::svref_2object(\$thing)->FLAGS & 12288 and 0 + $thing eq $thing
!ll&&!rl&&r
000
MC/DC0%0/3
B::svref_2object(\$thing)->FLAGS & 12288 and 0 + $thing eq $thing and $thing * 0 == 0
B::svref_2object(\$thing)->FLAGS & 12288 0 + $thing eq $thing $thing * 0 == 0
Truth table0%0/4
A and B and C
A= B::svref_2object(\$thing)->FLAGS & 12288B= 0 + $thing eq $thingC= $thing * 0 == 0
ABCresult
0XX0
10X0
1100
1111
144 && 0 + $thing eq $thing
145 && $thing * 0 == 0;
146 0 return 0;
147 }
148  
149 sub scan {
150 0 ▶ my ($self, %args) = @_;
Subroutine scan: not called CC=2 SCAR=17.9
Pod documented
151 0 ▶ $args{TableName} ||= $self->table;
Condition0%0/3
$args{"TableName"} ||= $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"TableName"} ||= $self->table
$args{"TableName"} $self->table
Truth table0%0/3
A ||= B
A= $args{"TableName"}B= $self->table
ABresult
1X1
011
000
152 0 return $self->dynamodb->scan(\%args);
153 }
154  
155 sub scan_as_hashref {
156 0 ▶ my ($self, %args) = @_;
Subroutine scan_as_hashref: not called CC=3 SCAR=24.8
Pod documented
157 0 ▶ $args{TableName} ||= $self->table;
Condition0%0/3
$args{"TableName"} ||= $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"TableName"} ||= $self->table
$args{"TableName"} $self->table
Truth table0%0/3
A ||= B
A= $args{"TableName"}B= $self->table
ABresult
1X1
011
000
158 0 my $items_hashref = {};
159 0 my $items_arrayref = $self->scan(%args);
160 0 my $hash_key_name = $self->hash_key;
161  
162 0 for my $item (@$items_arrayref) {
163 0 my $hash_key_value = delete $item->{$hash_key_name};
164 0 $items_hashref->{$hash_key_value} = $item;
165 }
166  
167 0 return $items_hashref;
168 }
169  
170 1 1;
171 __END__
↑ Summary Build »