File Coverage

blib/lib/DBI/Util/_accessor.pm
Criterion Covered Total %
statement 37 39 94.8
branch 7 14 50.0
condition 6 13 46.1
subroutine 10 10 100.0
pod 0 5 0.0
total 60 81 74.0


line stmt bran cond sub pod time code
1             package DBI::Util::_accessor;
2 52     52   281 use strict;
  52         272  
  52         1651  
3 52     52   196 use Carp;
  52         68  
  52         11434  
4             our $VERSION = "0.009479";
5              
6             # inspired by Class::Accessor::Fast
7              
8             sub new {
9 5661     5661 0 7197 my($proto, $fields) = @_;
10 5661   33     18838 my($class) = ref $proto || $proto;
11 5661   50     10251 $fields ||= {};
12              
13 5661   33     16973 my @dubious = grep { !m/^_/ && !$proto->can($_) } keys %$fields;
  31042         157915  
14 5661 50       12679 carp "$class doesn't have accessors for fields: @dubious" if @dubious;
15              
16             # make a (shallow) copy of $fields.
17 5661         32627 bless {%$fields}, $class;
18             }
19              
20             sub mk_accessors {
21 328     328 0 1030 my($self, @fields) = @_;
22 328         1888 $self->mk_accessors_using('make_accessor', @fields);
23             }
24              
25             sub mk_accessors_using {
26 484     484 0 1026 my($self, $maker, @fields) = @_;
27 484   33     1974 my $class = ref $self || $self;
28              
29             # So we don't have to do lots of lookups inside the loop.
30 484 50       4302 $maker = $self->can($maker) unless ref $maker;
31              
32 52     52   258 no strict 'refs';
  52         74  
  52         11292  
33 484         878 foreach my $field (@fields) {
34 2676         3489 my $accessor = $self->$maker($field);
35 2676         7906 *{$class."\:\:$field"} = $accessor
  2676         10236  
36 2676 50       2181 unless defined &{$class."\:\:$field"};
37             }
38             #my $hash_ref = \%{$class."\:\:_accessors_hash};
39             #$hash_ref->{$_}++ for @fields;
40             # XXX also copy down _accessors_hash of base class(es)
41             # so one in this class is complete
42 484         1496 return;
43             }
44              
45             sub make_accessor {
46 2520     2520 0 2497 my($class, $field) = @_;
47             return sub {
48 201766     201766   191147 my $self = shift;
49 201766 100       735764 return $self->{$field} unless @_;
50 28969 50       45378 croak "Too many arguments to $field" if @_ > 1;
51 28969         74164 return $self->{$field} = shift;
52 2520         6944 };
53             }
54              
55             sub make_accessor_autoviv_hashref {
56 156     156 0 246 my($class, $field) = @_;
57             return sub {
58 200     200   2659 my $self = shift;
59 200 50 100     864 return $self->{$field} ||= {} unless @_;
60 0 0         croak "Too many arguments to $field" if @_ > 1;
61 0           return $self->{$field} = shift;
62 156         554 };
63             }
64              
65             1;