Fey/Role/SQL/Cloneable.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 95.0% 57 / 60 CC 9 SCAR 11.4
CC
9
Cov
94%
__ANON__
5.0
__ANON__
3.0
clone
3.0
CRAP 3.1
« Fey/SQL/Fragment/Join.pm ↑ Summary Fey/FK.pm »
1 package Fey::Role::SQL::Cloneable;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 our $VERSION = '0.44';
8  
9 27 ▶use MooseX::Role::Parameterized 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 parameter 'real_class' => ( isa => 'Moose::Meta::Class' );
12  
13 # Yeah, I could've used MooseX::Clone, but avoiding the meta-API at
14 # runtime makes this all much faster. Of course, it's probably the
15 # root of all evil. OTOH, it's encapsulated in a role, so we can
16 # always replace it with an actual use of MX::Clone easily enough.
17 216 ▶role {
Subroutine __ANON__: called CC=5 SCAR=16.1
18 216 my $p = shift;
19 216 my %extra = @_;
20  
21 216 my @array_attr;
22 my @hash_attr;
23  
24 # XXX - hack to allow Fey::Role::SetOperation to get Cloneable
25 # applied to the real consuming class.
26 216 ▶ my $meta = $p->real_class() ? $p->real_class() : $extra{consumer};
Branch: 1 branch
BranchTrueFalseTotal
$p->real_class ? : 0 216 216
27  
28 216 for my $attr ( grep { $_->has_type_constraint() }
29 $meta->get_all_attributes() ) {
30 1134 my $type = $attr->type_constraint();
31  
32 1134 ▶ if ( $type->is_a_type_of('ArrayRef') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type->is_a_type_of('ArrayRef')) { } 702 432 1134
elsif ($type->is_a_type_of('HashRef')) { } 54 378 432
33 702 push @array_attr, $attr->name();
34 }
35 elsif ( $type->is_a_type_of('HashRef') ) {
36 54 push @hash_attr, $attr->name();
37 }
38 }
39  
40 7 ▶ method clone => sub {
Subroutine clone: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
41 7 my $self = shift;
42  
43 7 my $clone = bless { %{$self} }, ref $self;
44  
45 7 for my $name (@array_attr) {
46 21 $clone->{$name} = [ @{ $self->{$name} } ];
47 }
48  
49 7 for my $name (@hash_attr) {
50 0 $clone->{$name} = { %{ $self->{$name} } };
51 }
52  
53 7 return $clone;
54 216 };
55 24 };
56  
57 24 1;
58  
59 # ABSTRACT: Adds a just-deep-enough clone() method to SQL objects
60  
61 __END__
« Fey/SQL/Fragment/Join.pm ↑ Summary Fey/FK.pm »