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 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.42';
8  
9 26 ▶use MooseX::Role::Parameterized 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 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 role {
18 208 ▶ my $p = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
19 208 my %extra = @_;
20  
21 208 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 208 ▶ my $meta = $p->real_class() ? $p->real_class() : $extra{consumer};
Branch: 1 branch
BranchTrueFalseTotal
$p->real_class ? : 0 208 208
27  
28 208 for my $attr ( grep { $_->has_type_constraint() }
29 $meta->get_all_attributes() ) {
30 1092 my $type = $attr->type_constraint();
31  
32 1092 ▶ if ( $type->is_a_type_of('ArrayRef') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type->is_a_type_of('ArrayRef')) { } 676 416 1092
elsif ($type->is_a_type_of('HashRef')) { } 52 364 416
33 676 push @array_attr, $attr->name();
34 }
35 elsif ( $type->is_a_type_of('HashRef') ) {
36 52 push @hash_attr, $attr->name();
37 }
38 }
39  
40 method clone => sub {
41 7 ▶ my $self = shift;
Subroutine clone: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
42  
43 7 my $clone = bless { %{$self} }, ref $self;
44  
45 7 for my $name (@array_attr) {
46 24 $clone->{$name} = [ @{ $self->{$name} } ];
47 }
48  
49 7 for my $name (@hash_attr) {
50 2 $clone->{$name} = { %{ $self->{$name} } };
51 }
52  
53 7 return $clone;
54 208 };
55 23 };
56  
57 23 1;
58  
59 # ABSTRACT: Adds a just-deep-enough clone() method to SQL objects
60  
61 __END__
62  
63 =pod
64  
65 =head1 NAME
66  
67 Fey::Role::SQL::Cloneable - Adds a just-deep-enough clone() method to SQL objects
68  
69 =head1 VERSION
70  
71 version 0.42
72  
73 =head1 SYNOPSIS
74  
75 use Moose 0.90;
76  
77 with 'Fey::Role::SQL::Cloneable';
78  
79 =head1 DESCRIPTION
80  
81 Classes which do this role have a C<clone()> method which does a
82 just-deep-enough clone of the object.
83  
84 =head1 METHODS
85  
86 This role provides the following methods:
87  
88 =head2 $query->clone()
89  
90 Returns a new object which is a clone of the original.
91  
92 =head1 BUGS
93  
94 See L<Fey> for details on how to report bugs.
95  
96 =head1 AUTHOR
97  
98 Dave Rolsky <autarch@urth.org>
99  
100 =head1 COPYRIGHT AND LICENSE
101  
102 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
103  
104 This is free software, licensed under:
105  
106 The Artistic License 2.0 (GPL Compatible)
107  
108 =cut
« Fey/SQL/Fragment/Join.pm ↑ Summary Fey/FK.pm »