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 56     56   398 use strict;
  56         112  
  56         1483  
3 56     56   274 use Carp;
  56         107  
  56         12595  
4             our $VERSION = "0.009479";
5              
6             # inspired by Class::Accessor::Fast
7              
8             sub new {
9 7225     7225 0 13409 my($proto, $fields) = @_;
10 7225   33     21013 my($class) = ref $proto || $proto;
11 7225   50     13956 $fields ||= {};
12              
13 7225   33     23279 my @dubious = grep { !m/^_/ && !$proto->can($_) } keys %$fields;
  40378         175277  
14 7225 50       18049 carp "$class doesn't have accessors for fields: @dubious" if @dubious;
15              
16             # make a (shallow) copy of $fields.
17 7225         40007 bless {%$fields}, $class;
18             }
19              
20             sub mk_accessors {
21 352     352 0 1492 my($self, @fields) = @_;
22 352         1867 $self->mk_accessors_using('make_accessor', @fields);
23             }
24              
25             sub mk_accessors_using {
26 520     520 0 1492 my($self, $maker, @fields) = @_;
27 520   33     2209 my $class = ref $self || $self;
28              
29             # So we don't have to do lots of lookups inside the loop.
30 520 50       4090 $maker = $self->can($maker) unless ref $maker;
31              
32 56     56   414 no strict 'refs';
  56         118  
  56         13053  
33 520         1396 foreach my $field (@fields) {
34 2880         4932 my $accessor = $self->$maker($field);
35 2880         9965 *{$class."\:\:$field"} = $accessor
36 2880 50       4019 unless defined &{$class."\:\:$field"};
  2880         11662  
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 520         1628 return;
43             }
44              
45             sub make_accessor {
46 2712     2712 0 4393 my($class, $field) = @_;
47             return sub {
48 262098     262098   341710 my $self = shift;
49 262098 100       808546 return $self->{$field} unless @_;
50 41325 50       68484 croak "Too many arguments to $field" if @_ > 1;
51 41325         150580 return $self->{$field} = shift;
52 2712         8800 };
53             }
54              
55             sub make_accessor_autoviv_hashref {
56 168     168 0 381 my($class, $field) = @_;
57             return sub {
58 200     200   2281 my $self = shift;
59 200 50 100     728 return $self->{$field} ||= {} unless @_;
60 0 0         croak "Too many arguments to $field" if @_ > 1;
61 0           return $self->{$field} = shift;
62 168         753 };
63             }
64              
65             1;