NewDefaults/Magic.pm

filter: Statementstmt 100% 47 / 47 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 90.4% 66 / 73 CC 7 SCAR 9.7
CC
7
Cov
93%
new_default_for_attribute
4.1
__ANON__
4.0
BEGIN
1.0
CRAP 2.6
↑ Summary NewDefaults.pm »
1 #
2 # This file is part of MooseX-NewDefaults
3 #
4 # This software is Copyright (c) 2011 by Chris Weyl.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package MooseX::NewDefaults::Magic;
11 {
12 3 $MooseX::NewDefaults::Magic::VERSION = '0.004';
13 }
14  
15 # ABSTRACT: A slightly more magical version of NewDefaults
16  
17 3 ▶use Moose 0.94 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use MooseX::NewDefaults ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 {
25 3 package MooseX::NewDefaults::Trait::Class;
26 {
27 3 $MooseX::NewDefaults::Trait::Class::VERSION = '0.004';
28 }
29 3 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶ use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 ▶ sub new_default_for_attribute {
Subroutine new_default_for_attribute: called CC=4 SCAR=14.2
Pod undocumented
33 2 my ($self, $attribute_name, %options) = @_;
34  
35 ### find our attribute...
36 2 my $att = $self->find_attribute_by_name($attribute_name);
37 2 ▶ Moose->throw_error("Cannot find an attribute by the name of $attribute_name")
Branch: 1 branch
BranchTrueFalseTotal
unless $att 0 2 2
38 unless $att;
39  
40 2 ▶ unless (scalar keys %options) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %options) 2 0 2
41  
42 ### find our method and pull it...
43 2 ▶ my $method = $self->get_method($attribute_name)
Condition33%1/3
$self->get_method($attribute_name) || 'Moose'->throw_error("Cannot find locally defined method named: $attribute_name")
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->get_method($attribute_name) || 'Moose'->throw_error("Cannot find locally defined method named: $attribute_name")
$self->get_method($attribute_name) 'Moose'->throw_error("Cannot find locally defined method named: $attribute_name")
Truth table33%1/3
A || B
A= $self->get_method($attribute_name)B= 'Moose'->throw_error("Cannot find locally defined method named: $attribute_name")
ABresult
1X1
011
000
44 || Moose->throw_error("Cannot find locally defined method named: $attribute_name");
45  
46 2 $options{default} = $method->body;
47 2 $options{definition_context} = Moose::Util::_caller_info();
48  
49 2 require B;
50 2 my $cv = B::svref_2object($options{default});
51  
52 $options{definition_context} = {
53 2 package => $method->package_name,
54 file => $cv->GV->LINE, # $method->file,
55 line => $cv->FILE, #'unknown',
56 };
57  
58 ### and remove the method...
59 2 $self->remove_method($attribute_name);
60 }
61  
62 ### clone and extend the attribute with the new default...
63 2 $self->add_attribute("+$attribute_name", %options);
64  
65 ### tada!
66  
67 2 return;
68 }
69  
70 1 ▶ before make_immutable => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
71 1 my $self = shift @_;
72  
73 # check for local methods named the same as an attribute that are not
74 # accessors
75  
76 1 for my $name ($self->get_method_list) {
77  
78 4 my $method = $self->get_method($name);
79  
80 # skip if we're an accessor, or some other generated method
81 #next if $method->isa('Class::MOP::Method::Generated');
82 #next unless my $att = $self->find_attribute_by_name($name);
83  
84 4 ▶ $self->new_default_for_attribute($name)
Branch: 1 branch
BranchTrueFalseTotal
unless $method->isa("Class::MOP::Method::Generated") or not $self->find_attribute_by_name($name) 1 3 4
Condition100%3/3
$method->isa("Class::MOP::Method::Generated") or not $self->find_attribute_by_name($name)
l!l&&r!l&&!r
121
MC/DC100%2/2
$method->isa("Class::MOP::Method::Generated") or not $self->find_attribute_by_name($name)
$method->isa("Class::MOP::Method::Generated") not $self->find_attribute_by_name($name)
Truth table100%3/3
A or not(B)
A= $method->isa("Class::MOP::Method::Generated")B= not $self->find_attribute_by_name($name)
ABresult
1X1
011
000
85 unless $method->isa('Class::MOP::Method::Generated')
86 || !$self->find_attribute_by_name($name)
87 ;
88 }
89  
90 1 return;
91 3 };
92  
93 }
94  
95 Moose::Exporter->setup_import_methods(
96 3 also => [ qw{ MooseX::NewDefaults } ],
97 class_metaroles => {
98 class => [ 'MooseX::NewDefaults::Trait::Class' ],
99 },
100 );
101  
102 3 !!42;
103  
104 __END__
↑ Summary NewDefaults.pm »