MooseX/Traits/Attribute/MergeHashRef.pm

filter: Statementstmt 68.4% 26 / 38 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 67.9% 36 / 53 CC 2 SCAR 6.5
CC
2
Cov
64%
_inline_pre_body
6.0
__ANON__
2.0
__ANON__
1.0
CRAP 1.9
↑ Summary Moose/Meta/Attribute/Custom/Trait/MergeHashRef.pm »
1 #
2 # This file is part of MooseX-Traits-Attribute-MergeHashRef
3 #
4 # This software is Copyright (c) 2011 by Moritz Onken.
5 #
6 # This is free software, licensed under:
7 #
8 # The (three-clause) BSD License
9 #
10 package MooseX::Traits::Attribute::MergeHashRef;
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $MooseX::Traits::Attribute::MergeHashRef::VERSION = '1.002';
13 }
14 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 extends 'Moose::Meta::Method::Accessor';
16  
17 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Hash::Merge qw(merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶sub _inline_pre_body {
Subroutine _inline_pre_body: not called CC=2 SCAR=17.9
25 0 my $self = shift;
26 0 my $isa = $self->associated_attribute->{isa};
27 0 ▶ unless ( $isa eq 'HashRef' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isa eq "HashRef") 0 0 0
28 0 warn 'MergeHashRef work on HashRef attributes only';
29 0 return '';
30 }
31 0 my $inv = '$_[0]';
32  
33 0 my $attr = $self->associated_attribute;
34  
35 0 my $mi = $attr->associated_class->get_meta_instance;
36 0 my $pred = $mi->inline_is_slot_initialized( $inv, $attr->name );
37 0 my $old =
38 'my ($old) = '
39 . $pred . q{ ? }
40 . $self->_inline_get($inv)
41 . q{ : ()} . ";\n";
42 0 return $old . q{
43 use Hash::Merge qw(merge);
44 $_[1] = merge ($_[1], $old) if(defined $old && defined $_[1]);
45 };
46 }
47  
48 1 ▶override _generate_accessor_method_inline => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
49 1 my $self = shift;
50 1 my $attr = $self->associated_attribute;
51  
52 1 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 $self->_compile_code(
54 [ 'sub {',
55 'if (@_ > 1) {',
56 'use Hash::Merge qw(merge);',
57 '$_[1] = merge ($_[1], ',
58 $attr->_inline_instance_get('$_[0]'),
59 ') if(defined ',
60 $attr->_inline_instance_get('$_[0]'),
61 ' && defined $_[1]);',
62 $attr->_inline_set_value( '$_[0]', '$_[1]' ),
63 '}',
64 $attr->_inline_get_value('$_[0]'),
65 '}',
66 ] );
67 }
68 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 confess "Could not generate inline accessor because : $_";
70 1 };
71 3 ▶} unless( Moose->VERSION < 1.9);
Branch: 1 branch
BranchTrueFalseTotal
unless "Moose"->VERSION < "1.9" 3 0 3
72  
73 3 1;
74  
75  
76  
77 =pod
78  
79 =head1 NAME
80  
81 MooseX::Traits::Attribute::MergeHashRef
82  
83 =head1 VERSION
84  
85 version 1.002
86  
87 =head1 SYNOPSIS
88  
89 package MyClass;
90 use Moose;
91 has stash => ( is => 'rw', isa => 'HashRef', traits => [qw(MergeHashRef)] );
92  
93 my $class = MyClass->new;
94 $class->stash({ animals => { dogs => 1 } });
95 # $class->stash: { animals => { dogs => 1 } }
96 $class->stash({ animals => { cats => 2 } });
97 # $class->stash: { animals => { dogs => 1, cats => 2 } }
98 $class->set_stash({ foo => bar });
99 # $class->stash: { foo => bar });
100 $class->clear_stash;
101 # $class->stash: undef
102  
103 =head1 DESCRIPTION
104  
105 This trait will merge values added to a HashRef attribute. It uses L<Hash::Merge> to combine them.
106 The method C<set_$attr> which resets the attribute with a given hashref is also created.
107 Call C<clear_$attr> to clear the attribute.
108  
109 =head1 NAME
110  
111 MooseX::Traits::Attribute::MergeHashRef - Merging HashRef attribute
112  
113 =head1 AUTHOR
114  
115 Moritz Onken
116  
117 =head1 COPYRIGHT AND LICENSE
118  
119 This software is Copyright (c) 2011 by Moritz Onken.
120  
121 This is free software, licensed under:
122  
123 The (three-clause) BSD License
124  
125 =cut
126  
127  
128 __END__
↑ Summary Moose/Meta/Attribute/Custom/Trait/MergeHashRef.pm »