File Coverage

blib/lib/MooseX/Clone/Meta/Attribute/Trait/Copy.pm
Criterion Covered Total %
statement 19 22 86.3
branch 7 10 70.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 32 39 82.0


line stmt bran cond sub pod time code
1             package MooseX::Clone::Meta::Attribute::Trait::Copy;
2             # ABSTRACT: Simple copying of arrays and hashes for MooseX::Clone
3              
4             our $VERSION = '0.06';
5              
6 2     2   11 use Moose::Role;
  2         4  
  2         15  
7 2     2   11561 use Carp qw(croak);
  2         4  
  2         131  
8 2     2   14 use namespace::autoclean;
  2         4  
  2         20  
9              
10             with qw(MooseX::Clone::Meta::Attribute::Trait::Clone::Base);
11              
12 1     1   9447 sub Moose::Meta::Attribute::Custom::Trait::Copy::register_implementation { __PACKAGE__ }
13              
14             sub clone_value {
15 5     5 0 14 my ( $self, $target, $proto, %args ) = @_;
16              
17 5 100       16 if (exists $args{init_arg}) {
18 1         11 return $self->set_value( $target, $args{init_arg} );
19             }
20              
21 4 100       14 return unless $self->has_value($proto);
22              
23 2         52 my $clone = $self->_copy_ref($self->get_value($proto));
24              
25 2         7 $self->set_value( $target, $clone );
26             }
27              
28             sub _copy_ref {
29 2     2   185 my ( $self, $value ) = @_;
30              
31 2 50       14 if ( not ref $value ) {
    50          
    50          
32 0         0 return $value;
33             } elsif ( ref $value eq 'ARRAY' ) {
34 0         0 return [@$value];
35             } elsif ( ref $value eq 'HASH' ) {
36 2         9 return {%$value};
37             } else {
38 0           croak "The Copy trait is for arrays and hashes. Use the Clone trait for objects";
39             }
40             }
41              
42             __PACKAGE__
43              
44             __END__
45              
46             =pod
47              
48             =encoding UTF-8
49              
50             =head1 NAME
51              
52             MooseX::Clone::Meta::Attribute::Trait::Copy - Simple copying of arrays and hashes for MooseX::Clone
53              
54             =head1 VERSION
55              
56             version 0.06
57              
58             =head1 SYNOPSIS
59              
60             has foo => (
61             isa => "ArrayRef",
62             traits => [qw(Copy)],
63             );
64              
65             =head1 DESCRIPTION
66              
67             Unlike the C<Clone> trait, which does deep copying of almost anything, this
68             trait will only do one additional level of copying of arrays and hashes.
69              
70             This is both simpler and faster when you don't need a real deep copy of the
71             entire structure, and probably more correct.
72              
73             =head1 AUTHOR
74              
75             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
76              
77             =head1 COPYRIGHT AND LICENSE
78              
79             This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman).
80              
81             This is free software; you can redistribute it and/or modify it under
82             the same terms as the Perl 5 programming language system itself.
83              
84             =cut