File Coverage

blib/lib/DBIx/QuickDB/Util/HashBase.pm
Criterion Covered Total %
statement 79 87 90.8
branch 28 34 82.3
condition 8 17 47.0
subroutine 14 17 82.3
pod 1 1 100.0
total 130 156 83.3


line stmt bran cond sub pod time code
1             package DBIx::QuickDB::Util::HashBase;
2 20     20   92701 use strict;
  20         39  
  20         868  
3 20     20   99 use warnings;
  20         38  
  20         1532  
4              
5             our $VERSION = '0.000039';
6              
7             #################################################################
8             # #
9             # This is a generated file! Do not modify this file directly! #
10             # Use hashbase_inc.pl script to regenerate this file. #
11             # The script is part of the Object::HashBase distribution. #
12             # Note: You can modify the version number above this comment #
13             # if needed, that is fine. #
14             # #
15             #################################################################
16              
17             {
18 20     20   148 no warnings 'once';
  20         56  
  20         2669  
19             $DBIx::QuickDB::Util::HashBase::HB_VERSION = '0.009';
20             *DBIx::QuickDB::Util::HashBase::ATTR_SUBS = \%Object::HashBase::ATTR_SUBS;
21             *DBIx::QuickDB::Util::HashBase::ATTR_LIST = \%Object::HashBase::ATTR_LIST;
22             *DBIx::QuickDB::Util::HashBase::VERSION = \%Object::HashBase::VERSION;
23             *DBIx::QuickDB::Util::HashBase::CAN_CACHE = \%Object::HashBase::CAN_CACHE;
24             }
25              
26              
27             require Carp;
28             {
29 20     20   125 no warnings 'once';
  20         38  
  20         2176  
30             $Carp::Internal{+__PACKAGE__} = 1;
31             }
32              
33             BEGIN {
34             # these are not strictly equivalent, but for out use we don't care
35             # about order
36             *_isa = ($] >= 5.010 && require mro) ? \&mro::get_linear_isa : sub {
37 20     20   125 no strict 'refs';
  20         50  
  20         2872  
38 0         0 my @packages = ($_[0]);
39 0         0 my %seen;
40 0         0 for my $package (@packages) {
41 0         0 push @packages, grep !$seen{$_}++, @{"$package\::ISA"};
  0         0  
42             }
43 0         0 return \@packages;
44             }
45 20 50 33 20   12164 }
46              
47             my %SPEC = (
48             '^' => {reader => 1, writer => 0, dep_writer => 1, read_only => 0, strip => 1},
49             '-' => {reader => 1, writer => 0, dep_writer => 0, read_only => 1, strip => 1},
50             '>' => {reader => 0, writer => 1, dep_writer => 0, read_only => 0, strip => 1},
51             '<' => {reader => 1, writer => 0, dep_writer => 0, read_only => 0, strip => 1},
52             '+' => {reader => 0, writer => 0, dep_writer => 0, read_only => 0, strip => 1},
53             );
54              
55             sub import {
56 96     96   12426 my $class = shift;
57 96         246 my $into = caller;
58              
59             # Make sure we list the OLDEST version used to create this class.
60 96   33     366 my $ver = $DBIx::QuickDB::Util::HashBase::HB_VERSION || $DBIx::QuickDB::Util::HashBase::VERSION;
61 96 50 33     520 $DBIx::QuickDB::Util::HashBase::VERSION{$into} = $ver if !$DBIx::QuickDB::Util::HashBase::VERSION{$into} || $DBIx::QuickDB::Util::HashBase::VERSION{$into} > $ver;
62              
63 96         3185 my $isa = _isa($into);
64 96   50     573 my $attr_list = $DBIx::QuickDB::Util::HashBase::ATTR_LIST{$into} ||= [];
65 96   50     480 my $attr_subs = $DBIx::QuickDB::Util::HashBase::ATTR_SUBS{$into} ||= {};
66              
67             my %subs = (
68             ($into->can('new') ? () : (new => \&_new)),
69 85 50       4316 (map %{$DBIx::QuickDB::Util::HashBase::ATTR_SUBS{$_} || {}}, @{$isa}[1 .. $#$isa]),
  96         315  
70             (
71             map {
72 96 100       1343 my $p = substr($_, 0, 1);
  633         1284  
73 633         962 my $x = $_;
74              
75 633   100     1669 my $spec = $SPEC{$p} || {reader => 1, writer => 1};
76              
77 633 100       1625 substr($x, 0, 1) = '' if $spec->{strip};
78 633         1352 push @$attr_list => $x;
79 633         1502 my ($sub, $attr) = (uc $x, $x);
80              
81 633     0   4294 $attr_subs->{$sub} = sub() { $attr };
  0         0  
82 633         1389 my %out = ($sub => $attr_subs->{$sub});
83              
84 633 100   17   2582 $out{$attr} = sub { $_[0]->{$attr} } if $spec->{reader};
  17     0   125  
85 633 100   8   1517 $out{"set_$attr"} = sub { $_[0]->{$attr} = $_[1] } if $spec->{writer};
  8     0   65  
86 633 100   1   1763 $out{"set_$attr"} = sub { Carp::croak("'$attr' is read-only") } if $spec->{read_only};
  1         205  
87 633 100   1   1258 $out{"set_$attr"} = sub { Carp::carp("set_$attr() is deprecated"); $_[0]->{$attr} = $_[1] } if $spec->{dep_writer};
  1         1037  
  1         16  
88              
89 633         2735 %out;
90             } @_
91             ),
92             );
93              
94 20     20   154 no strict 'refs';
  20         44  
  20         12570  
95 96         721 *{"$into\::$_"} = $subs{$_} for keys %subs;
  2434         174836  
96             }
97              
98             sub attr_list {
99 3     3 1 1766 my $class = shift;
100              
101 3         20 my $isa = _isa($class);
102              
103 3         8 my %seen;
104 15         43 my @list = grep { !$seen{$_}++ } map {
105 3         8 my @out;
  6         11  
106              
107 6 50 50     33 if (0.004 > ($DBIx::QuickDB::Util::HashBase::VERSION{$_} || 0)) {
108 0         0 Carp::carp("$_ uses an inlined version of DBIx::QuickDB::Util::HashBase too old to support attr_list()");
109             }
110             else {
111 6         14 my $list = $DBIx::QuickDB::Util::HashBase::ATTR_LIST{$_};
112 6 50       22 @out = $list ? @$list : ()
113             }
114              
115 6         19 @out;
116             } reverse @$isa;
117              
118 3         22 return @list;
119             }
120              
121             sub _new {
122 11     11   240258 my $class = shift;
123              
124 11         24 my $self;
125              
126 11 100       39 if (@_ == 1) {
127 3         6 my $arg = shift;
128 3         9 my $type = ref($arg);
129              
130 3 100       10 if ($type eq 'HASH') {
131 1         5 $self = bless({%$arg}, $class)
132             }
133             else {
134 2 50       10 Carp::croak("Not sure what to do with '$type' in $class constructor")
135             unless $type eq 'ARRAY';
136              
137 2         5 my %proto;
138 2         6 my @attributes = attr_list($class);
139 2         39 while (@$arg) {
140 9         19 my $val = shift @$arg;
141 9 100       172 my $key = shift @attributes or Carp::croak("Too many arguments for $class constructor");
142 8         21 $proto{$key} = $val;
143             }
144              
145 1         5 $self = bless(\%proto, $class);
146             }
147             }
148             else {
149 8         35 $self = bless({@_}, $class);
150             }
151              
152             $DBIx::QuickDB::Util::HashBase::CAN_CACHE{$class} = $self->can('init')
153 10 100       73 unless exists $DBIx::QuickDB::Util::HashBase::CAN_CACHE{$class};
154              
155 10 100       64 $self->init if $DBIx::QuickDB::Util::HashBase::CAN_CACHE{$class};
156              
157 10         48 $self;
158             }
159              
160             1;
161              
162             __END__