blib/lib/Class/Accessor/Fast/Contained.pm

filter: Statementstmt 100% 44 / 44 Branchbran 68.1% 15 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 91.3% 74 / 81 CC 12 SCAR 10.9
CC
12
Cov
89%
__ANON__
4.1
new
4.1
__ANON__
4.1
CRAP 3.0
↑ Summary
1 package Class::Accessor::Fast::Contained;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '1.01';
9 6 $VERSION = eval $VERSION; # numify for warning-free dev releases
10  
11 6 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # this module does two things differently to the venerable
14 # Class::Accessor::Fast,
15 # 1) fields are stored at arms-length in a single key of $self
16 # 2) new() allows mixin into an existing object
17  
18 7 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
19 7 my ($class, $fields) = @_;
20  
21 7 ▶ $fields = {} unless defined $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 2 5 7
22  
23 7 ▶ my $self = (ref $class ? $class : bless {}, $class);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 7 7
24  
25 7 ▶ my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
Branch: 1 branch
BranchTrueFalseTotal
"$self" =~ /=GLOB/ ? : 0 7 7
26 7 $copy->{ref $self} = {%$fields};
27  
28 7 return $self;
29 }
30  
31 6 *{Symbol::qualify_to_ref('setup')} = \&new;
32  
33 11 ▶sub make_accessor {
Subroutine make_accessor: called CC=1 SCAR=0.0
Pod documented
34 11 my($class, $field) = @_;
35  
36 14 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
37 14 my $self = shift;
38 14 ▶ my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
Branch: 1 branch
BranchTrueFalseTotal
"$self" =~ /=GLOB/ ? : 0 14 14
39 14 ▶ return $copy->{ref $self}->{$field} if scalar @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 0 9 5 14
40 5 ▶ $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 0 5
41 11 };
42 }
43  
44  
45 9 ▶sub make_ro_accessor {
Subroutine make_ro_accessor: called CC=1 SCAR=0.0
Pod documented
46 9 my($class, $field) = @_;
47  
48 7 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
49 7 my $self = shift;
50 7 ▶ my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
Branch: 1 branch
BranchTrueFalseTotal
"$self" =~ /=GLOB/ ? : 0 7 7
51 7 ▶ return $copy->{ref $self}->{$field} if scalar @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 0 5 2 7
52 2 my $caller = caller;
53 2 $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
54 9 };
55 }
56  
57 9 ▶sub make_wo_accessor {
Subroutine make_wo_accessor: called CC=1 SCAR=0.0
Pod documented
58 9 my($class, $field) = @_;
59  
60 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
61 5 my $self = shift;
62 5 ▶ my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
Branch: 1 branch
BranchTrueFalseTotal
"$self" =~ /=GLOB/ ? : 0 5 5
63  
64 5 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 2 5
65 2 my $caller = caller;
66 2 $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
67 }
68 else {
69 3 ▶ return $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 0 3
70 }
71 9 };
72 }
73  
74 =head1 NAME
75  
76 Class::Accessor::Fast::Contained - Fast accessors with data containment
77  
78 =head1 VERSION
79  
80 This document refers to version 1.01 of Class::Accessor::Fast::Contained
81  
82 =head1 SYNOPSIS
83  
84 package Foo;
85 use base qw(Class::Accessor::Fast::Contained);
86  
87 # The rest is the same as Class::Accessor::Fast
88  
89 =head1 DESCRIPTION
90  
91 This module does two things differently to the venerable Class::Accessor::Fast :
92  
93 =over 4
94  
95 =item *
96  
97 Fields are stored at arms-length within a single hash value of $self, rather
98 than directly in the $self blessed referent.
99  
100 =item *
101  
102 C<new()> allows mixin into an existing object, rather than creating and
103 returning a new blessed hashref. To do this, just call something like:
104  
105 my $self = Some::Other::Class->new;
106 $self = $self->Class::Accessor::Fast::Contained::new;
107  
108 Note that the mixin code only supports objects which use a blessed hash
109 reference or a blessed typeglob reference.
110  
111 An alias C<setup()> is available which does the same as C<new()> but might
112 make more sense if being used in this way.
113  
114 =back
115  
116 =head1 DEPENDENCIES
117  
118 Other than the standard Perl distribution, you will need the following:
119  
120 =over 4
121  
122 =item *
123  
124 Class::Accessor
125  
126 =back
127  
128 =head1 BUGS
129  
130 If you spot a bug or are experiencing difficulties that are not explained
131 within the documentation, please send an email to oliver@cpan.org or submit a
132 bug to the RT system (http://rt.cpan.org/). It would help greatly if you are
133 able to pinpoint problems or even supply a patch.
134  
135 =head1 SEE ALSO
136  
137 L<Class::Accessor>
138  
139 =head1 AUTHOR
140  
141 Oliver Gorwits C<< <oliver.gorwits@oucs.ox.ac.uk> >>
142  
143 =head1 ACKNOWLEDGEMENTS
144  
145 Thanks to Marty Pauly and Michael G Schwern for L<Class::Accessor> and its
146 tests, which I've shamelessly borrowed for this distribution.
147  
148 =head1 COPYRIGHT & LICENSE
149  
150 Copyright (c) The University of Oxford 2008.
151  
152 This library is free software; you can redistribute it and/or modify it under
153 the same terms as Perl itself.
154  
155 =cut
156  
157 6 1;
158  
↑ Summary