lib/Class/Accessor/Fast/WithBuilder.pm

filter: Statementstmt 100% 31 / 31 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 96.4% 55 / 57 CC 9 SCAR 10.7
CC
9
Cov
96%
__ANON__
5.2
__ANON__
3.0
_mk_accessors
3.0
CRAP 2.9
↑ Summary
1 package Class::Accessor::Fast::WithBuilder;
2  
3 =head1 NAME
4  
5 Class::Accessor::Fast::WithBuilder - Class::Accessor::Fast with lazy defaults
6  
7 =head1 VERSION
8  
9 0.0101
10  
11 =head1 DESCRIPTION
12  
13 This module will require builders for each attribute defined. This means
14 that adding attributes with this module, is something like this code
15 from L<Moose>:
16  
17 has age => (
18 is => "ro", # or rw
19 lazy => 1,
20 builder => "_build_age",
21 );
22  
23 NOTE: All builders will be called as late as possible.
24  
25 =head1 SYNOPSIS
26  
27 package Foo;
28 use base qw(Class::Accessor::Fast::WithBuilder);
29  
30 Foo->mk_accessors(qw( name age ));
31  
32 sub _build_name { $_[0]->_croak("'name' attribute cannot be built!") }
33 sub _build_age { 0 }
34  
35 print Foo->new->name; # BOOM!
36 print Foo->new->age; # Will print "0"
37 print Foo->new({ age => 123 })->age; # Will print "123"
38  
39 =cut
40  
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use base 'Class::Accessor::Fast'; # overriding all ::Fast methods, but...
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 our $VERSION = '0.0101';
46  
47 =head1 METHODS
48  
49 =head2 make_accessor
50  
51 See L<Class::Accessor/make_accessor>
52  
53 =cut
54  
55 1 ▶sub make_accessor {
Subroutine make_accessor: called CC=1 SCAR=0.0
Pod documented
56 1 my($class, $field) = @_;
57 1 my $builder = "_build_$field";
58  
59 3 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
60 3 ▶ return $_[0]->{$field} = @_ == 2 ? $_[1] : [@_[1..$#_]] if @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 1 2 3
@_ == 2 ? : 1 0 1
61 2 ▶ exists $_[0]->{$field} or $_[0]->{$field} = $_[0]->$builder;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{$field} or $_[0]{$field} = $_[0]->$builder 1 1 2
62 2 ▶ return $_[0]->{$field} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 0 2
63 1 };
64 }
65  
66 =head2 make_ro_accessor
67  
68 See L<Class::Accessor/make_ro_accessor>
69  
70 =cut
71  
72 1 ▶sub make_ro_accessor {
Subroutine make_ro_accessor: called CC=1 SCAR=0.0
Pod documented
73 1 my($class, $field) = @_;
74 1 my $builder = "_build_$field";
75  
76 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
77 3 ▶ exists $_[0]->{$field} or $_[0]->{$field} = $_[0]->$builder;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{$field} or $_[0]{$field} = $_[0]->$builder 2 1 3
78 3 ▶ return $_[0]->{$field} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 1 3
79 1 my $caller = caller;
80 1 $_[0]->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
81 1 };
82 }
83  
84 =head2 make_wo_accessor
85  
86 This is not implemented. (See L<Class::Accessor/make_wo_accessor>)
87  
88 =cut
89  
90 1 ▶sub make_wo_accessor {
Subroutine make_wo_accessor: called CC=1 SCAR=0.0
Pod documented
91 1 $_[0]->_croak('not implemented');
92 }
93  
94 # used to check if _build_foo() methods are defined
95 3 ▶sub _mk_accessors {
Subroutine _mk_accessors: called CC=3 SCAR=11.0
96 3 my $class = shift;
97 3 my($type, @fields) = @_;
98  
99 3 for my $f (@fields) {
100 3 ▶ unless($class->can("_build_$f")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("_build_$f")) 1 2 3
101 1 $class->_croak("$class\::_build_$f() is required!");
102 }
103 }
104  
105 2 return $class->SUPER::_mk_accessors(@_);
106 }
107  
108 =head1 COPYRIGHT & LICENSE
109  
110 =head1 AUTHOR
111  
112 Jan Henning Thorsen C<< jhthorsen at cpan.org >>
113  
114 =cut
115  
116 1 1;
↑ Summary