blib/lib/Object/Annotate.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 63.8% 23 / 36 Conditioncond 45.1% 23 / 51 MC/DCmcdc 10.0% 3 / 30 Subroutinesub 85.7% 18 / 21 Podpod 100% 11 / 11 totaltotal 69.7% 189 / 271 SCAR 41.0
CC
38
Cov
75%
setup_class
22.8
__ANON__
8.4
construct_cdbi_class
7.4
CRAP 60.2
↑ Summary
1 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Object::Annotate;
4 {
5 3 $Object::Annotate::VERSION = '0.024';
6 }
7 # ABSTRACT: mix in logging-to-database to objects (deprecated)
8  
9 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use UNIVERSAL::moniker 0.01;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # We'll store the constructed Class::DBI subclasses here.
14 # $class_for->{ $dsn }->{ $table } = $class
15 3 my $class_for = {};
16  
17 # We'll keep a counter, here, to use to form unique class names.
18 3 my $current_suffix = 0;
19  
20 # The "id" column isn't here because we want it first, always.
21 3 my %note_columns = (
22 mandatory => [ qw(class object_id created) ],
23 # I plan to use these values in the future. -- rjbs, 2006-01-13
24 # default => [ qw(event attr old_val new_val via comment expire_time) ],
25 default => [ qw(event attr old_val new_val via comment expire_time) ],
26 );
27  
28 3 use Sub::Exporter 0.92 -setup => {
29 groups => { annotator => \&setup_class },
30 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 sub new {
34 0 ▶ my ($self, $arg) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
35 0 ▶ my $class = (ref $self) ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
36  
37 0 my $target
38 = sprintf '%s::Singularity::0x%08x', $class, ++$current_suffix;
39  
40 0 $self->setup_class($target, $arg);
41  
42 0 my $singularity = \do { undef };
43 0 bless $singularity => $target;
44 }
45  
46  
47  
48 sub setup_class {
49 3 ▶ my ($self, $name, $arg, $col) = @_;
Subroutine setup_class: called CC=12 SCAR=31.3
Pod documented
50  
51 3 ▶ $arg->{db}{dsn} ||= $self->default_dsn;
Condition33%1/3
$arg->{"db"}{"dsn"} ||= $self->default_dsn
l!l&&r!l&&!r
300
MC/DC0%0/2
$arg->{"db"}{"dsn"} ||= $self->default_dsn
$arg->{"db"}{"dsn"} $self->default_dsn
Truth table33%1/3
A ||= B
A= $arg->{"db"}{"dsn"}B= $self->default_dsn
ABresult
1X1
011
000
52 3 ▶ $arg->{db}{table} ||= $self->default_table;
Condition33%1/3
$arg->{"db"}{"table"} ||= $self->default_table
l!l&&r!l&&!r
300
MC/DC0%0/2
$arg->{"db"}{"table"} ||= $self->default_table
$arg->{"db"}{"table"} $self->default_table
Truth table33%1/3
A ||= B
A= $arg->{"db"}{"table"}B= $self->default_table
ABresult
1X1
011
000
53  
54 3 ▶ $arg->{db}{user} ||= $self->default_user;
Condition33%1/3
$arg->{"db"}{"user"} ||= $self->default_user
l!l&&r!l&&!r
003
MC/DC0%0/2
$arg->{"db"}{"user"} ||= $self->default_user
$arg->{"db"}{"user"} $self->default_user
Truth table33%1/3
A ||= B
A= $arg->{"db"}{"user"}B= $self->default_user
ABresult
1X1
011
000
55 3 ▶ $arg->{db}{pass} ||= $self->default_pass;
Condition33%1/3
$arg->{"db"}{"pass"} ||= $self->default_pass
l!l&&r!l&&!r
003
MC/DC0%0/2
$arg->{"db"}{"pass"} ||= $self->default_pass
$arg->{"db"}{"pass"} $self->default_pass
Truth table33%1/3
A ||= B
A= $arg->{"db"}{"pass"}B= $self->default_pass
ABresult
1X1
011
000
56  
57 3 ▶ $arg->{db}{sequence} ||= $self->_default_sequence;
Condition33%1/3
$arg->{"db"}{"sequence"} ||= $self->_default_sequence
l!l&&r!l&&!r
003
MC/DC0%0/2
$arg->{"db"}{"sequence"} ||= $self->_default_sequence
$arg->{"db"}{"sequence"} $self->_default_sequence
Truth table33%1/3
A ||= B
A= $arg->{"db"}{"sequence"}B= $self->_default_sequence
ABresult
1X1
011
000
58  
59 3 ▶ if ($arg->{noun} xor $arg->{verb}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg->{'noun'} xor $arg->{'verb'}) { } 0 3 3
elsif (not $arg->{'noun'} || $arg->{'verb'}) { } 3 0 3
Condition25%1/4
$arg->{"noun"} xor $arg->{"verb"}
l&&rl&&!r!l&&r!l&&!r
0003
MC/DC0%0/2
$arg->{"noun"} xor $arg->{"verb"}
$arg->{"noun"} $arg->{"verb"}
Truth table25%1/4
A xor B
A= $arg->{"noun"}B= $arg->{"verb"}
ABresult
110
101
011
000
60 0 Carp::croak 'you must supply either both or neither "noun" and "verb"';
61 ▶ } elsif (not ($arg->{noun} or $arg->{verb})) {
Condition33%1/3
$arg->{'noun'} || $arg->{'verb'}
l!l&&r!l&&!r
003
MC/DC0%0/2
$arg->{'noun'} || $arg->{'verb'}
$arg->{'noun'} $arg->{'verb'}
Truth table33%1/3
A || B
A= $arg->{'noun'}B= $arg->{'verb'}
ABresult
1X1
011
000
62 3 @$arg{qw(noun verb)} = qw(annotations annotate);
63 }
64  
65 3 my $class = $self->class_for($arg);
66  
67 3 my $obj_class = $arg->{obj_class};
68  
69 my %build_option = (
70 obj_class => $obj_class,
71 id_attr => $arg->{id_attr} || 'id',
72  
73 noun => $arg->{noun},
74 verb => $arg->{verb},
75 3 ▶ );
Condition100%2/2
$arg->{'id_attr'} || 'id'
l!l
12
MC/DC100%1/1
$arg->{'id_attr'} || 'id'
$arg->{'id_attr'}
Truth table100%2/2
A
A= $arg->{'id_attr'}
Aresult
11
00
76  
77 my $annotator = $self->build_annotator({
78 %build_option,
79 columns => $arg->{columns},
80 3 ▶ set_time => ($arg->{db}{dsn} && (scalar $arg->{db}{dsn} =~ /SQLite/)),
Condition50%1/2
$arg->{'db'}{'dsn'} && scalar $arg->{'db'}{'dsn'} =~ /SQLite/
!ll
03
MC/DC0%0/1
$arg->{'db'}{'dsn'} && scalar $arg->{'db'}{'dsn'} =~ /SQLite/
$arg->{'db'}{'dsn'}
Truth table50%1/2
A
A= $arg->{'db'}{'dsn'}
Aresult
00
11
81 });
82  
83 my $return = {
84 25 ▶ "$arg->{noun}_class" => sub { $class },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 3 $arg->{verb} => $annotator,
86 "search_$arg->{noun}" => $self->build_searcher(\%build_option),
87 };
88 }
89  
90  
91 sub class_for {
92 3 ▶ my ($self, $arg) = @_;
Subroutine class_for: called CC=6 SCAR=18.2
Pod documented
93  
94 3 my $dsn = $arg->{db}{dsn};
95 3 my $table = $arg->{db}{table};
96  
97 3 my $user = $arg->{db}{user};
98 3 my $pass = $arg->{db}{pass};
99  
100 # Try to find an already-constructed class.
101 my $class = ! $arg->{extra_setup}
102 && exists $class_for->{ $dsn }
103 && exists $class_for->{ $dsn }->{ $table }
104 3 ▶ && $class_for->{ $dsn }->{ $table };
Condition66%6/9
!$arg->{'extra_setup'} && exists $class_for->{$dsn} && exists $class_for->{$dsn}{$table} && $class_for->{$dsn}{$table}
!ll&&!rl&&r
102
not $arg->{"extra_setup"} and exists $class_for->{$dsn} and exists $class_for->{$dsn}{$table}
!ll&&!rl&&r
102
not $arg->{"extra_setup"} and exists $class_for->{$dsn}
!ll&&!rl&&r
012
MC/DC25%1/4
!$arg->{'extra_setup'} && exists $class_for->{$dsn} && exists $class_for->{$dsn}{$table} && $class_for->{$dsn}{$table}
not $arg->{"extra_setup"} exists $class_for->{$dsn} exists $class_for->{$dsn}{$table} $class_for->{$dsn}{$table}
Truth table40%2/5
not(A) and B and C && D
A= not $arg->{"extra_setup"}B= exists $class_for->{$dsn}C= exists $class_for->{$dsn}{$table}D= $class_for->{$dsn}{$table}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
105  
106 3 ▶ return $class if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 2 1 3
107  
108 # If we have no class built for this combination, build it.
109 $class = $self->construct_cdbi_class({
110 dsn => $dsn,
111 user => $user,
112 pass => $pass,
113 table => $table,
114 columns => $arg->{columns},
115 sequence => $arg->{db}{sequence},
116 base_class => $arg->{base_class},
117 1 });
118  
119 1 ▶ $arg->{extra_setup}->($class) if $arg->{extra_setup};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"extra_setup"} 0 1 1
120  
121 1 return $class;
122 }
123  
124  
125 0 ▶sub default_dsn { $ENV{OBJ_ANNOTATE_DSN}; }
Subroutine default_dsn: not called CC=1 SCAR=6.9
Pod documented
126 0 ▶sub default_table { $ENV{OBJ_ANNOTATE_TABLE}; }
Subroutine default_table: not called CC=1 SCAR=6.9
Pod documented
127 3 ▶sub default_user { $ENV{OBJ_ANNOTATE_USER}; }
Subroutine default_user: called CC=1 SCAR=0.0
Pod documented
128 3 ▶sub default_pass { $ENV{OBJ_ANNOTATE_PASS}; }
Subroutine default_pass: called CC=1 SCAR=0.0
Pod documented
129 1 ▶sub default_base_class { 'Class::DBI' }
Subroutine default_base_class: called CC=1 SCAR=0.0
Pod documented
130  
131 ▶sub _default_sequence { }
Subroutine _default_sequence: called CC=1 SCAR=0.0
132  
133  
134 sub construct_cdbi_class {
135 1 ▶ my ($class, $arg) = @_;
Subroutine construct_cdbi_class: called CC=7 SCAR=20.0
Pod documented
136  
137 1 my $new_class
138 = sprintf '%s::Construct::0x%08x', __PACKAGE__, ++$current_suffix;
139  
140 1 ▶ $arg->{base_class} ||= $class->default_base_class;
Condition33%1/3
$arg->{"base_class"} ||= $class->default_base_class
l!l&&r!l&&!r
010
MC/DC0%0/2
$arg->{"base_class"} ||= $class->default_base_class
$arg->{"base_class"} $class->default_base_class
Truth table33%1/3
A ||= B
A= $arg->{"base_class"}B= $class->default_base_class
ABresult
1X1
011
000
141  
142 1 ▶ eval "require $arg->{base_class};" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval qq[require $arg->{"base_class"};] or die $@ 0 1 1
143 1 do {
144 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 @{$new_class . '::ISA'} = $arg->{base_class};
146 };
147  
148 1 ▶ if ($arg->{dsn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"dsn"}) 1 0 1
149 1 $new_class->connection($arg->{dsn}, $arg->{user}, $arg->{pass});
150 }
151  
152 1 $new_class->table($arg->{table});
153  
154 1 my @columns = @{ $note_columns{mandatory} };
155 1 ▶ my @extra_columns = @{ $arg->{columns} || $note_columns{default} };
Branch: 1 branch
BranchTrueFalseTotal
$arg->{"columns"} or $note_columns{"default"} 1 0 1
156 1 push @columns, @extra_columns;
157  
158 1 $new_class->columns(All => ('id', @columns));
159  
160 1 ▶ $new_class->sequence($arg->{sequence}) if $arg->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"sequence"} 0 1 1
161  
162 1 $new_class->db_Main->{ AutoCommit } = 1;
163  
164 1 ▶ return $class_for->{ $arg->{dsn} || '' }->{ $arg->{table} } = $new_class;
Condition50%1/2
$arg->{'dsn'} || ''
l!l
10
MC/DC0%0/1
$arg->{'dsn'} || ''
$arg->{'dsn'}
Truth table50%1/2
A
A= $arg->{'dsn'}
Aresult
11
00
165 }
166  
167  
168 sub build_annotator {
169 3 ▶ my ($self, $arg) = @_;
Subroutine build_annotator: called CC=2 SCAR=7.0
Pod documented
170  
171 3 my $obj_class = $arg->{obj_class};
172 3 my $id_attr = $arg->{id_attr};
173 3 my $set_time = $arg->{set_time};
174  
175 my @columns
176 3 ▶ = $arg->{columns} ? @{ $arg->{columns} } : @{ $note_columns{default} };
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'columns'} ? : 0 3 3
177  
178 3 my $noun = $arg->{noun};
179  
180 my $annotator = sub {
181 # This $arg purposefully shadows the previous; I don't want to enclose
182 # those args. -- rjbs, 2006-01-05
183 9 ▶ my ($self, $arg) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.7
184 9 ▶ my $obj_class = $arg->{obj_class} || $self->moniker;
Condition33%1/3
$arg->{'obj_class'} || $self->moniker
l!l&&r!l&&!r
090
MC/DC0%0/2
$arg->{'obj_class'} || $self->moniker
$arg->{'obj_class'} $self->moniker
Truth table33%1/3
A || B
A= $arg->{'obj_class'}B= $self->moniker
ABresult
1X1
011
000
185  
186 9 my $id;
187 9 ▶ if (ref $id_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id_attr) { } 5 4 9
188 5 $id = $$id_attr;
189 } else {
190 4 $id = $self->$id_attr;
191 4 ▶ Carp::croak "couldn't get id for $self via $id_attr" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 4 4
192 }
193  
194 # build up only those attributes we declared
195 9 my %attr;
196 9 for (@columns) {
197 63 ▶ next unless exists $arg->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{$_} 46 17 63
198 17 $attr{$_} = $arg->{$_};
199 }
200  
201 9 ▶ $attr{created} = time if $set_time;
Branch: 1 branch
BranchTrueFalseTotal
if $set_time 9 0 9
202  
203 9 my $class_name_method = "$noun\_class";
204 9 my $request = $self->$class_name_method->create({
205 class => $obj_class,
206 object_id => $id,
207 %attr,
208 });
209  
210 9 return $request;
211 3 };
212  
213 3 return $annotator;
214 }
215  
216  
217 sub build_searcher {
218 3 ▶ my ($self, $arg) = @_;
Subroutine build_searcher: called CC=1 SCAR=0.0
Pod documented
219  
220 3 my $obj_class = $arg->{obj_class};
221 3 my $id_attr = $arg->{id_attr};
222  
223 3 my $noun = $arg->{noun};
224  
225 my $searcher = sub {
226 10 ▶ my ($self, $arg) = @_;
Subroutine __ANON__: called CC=8 SCAR=21.2
227 10 ▶ my $obj_class = $arg->{obj_class} || $self->moniker;
Condition33%1/3
$arg->{'obj_class'} || $self->moniker
l!l&&r!l&&!r
0100
MC/DC0%0/2
$arg->{'obj_class'} || $self->moniker
$arg->{'obj_class'} $self->moniker
Truth table33%1/3
A || B
A= $arg->{'obj_class'}B= $self->moniker
ABresult
1X1
011
000
228 10 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
100
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
229  
230 10 my $id;
231 10 ▶ if (ref $id_attr) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $id_attr) { } 3 7 10
elsif (ref $self) { } 5 2 7
232 3 $id = $$id_attr;
233 } elsif (ref $self) {
234 5 $id = $self->$id_attr;
235 5 ▶ Carp::croak "couldn't get id for $self via $id_attr" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 5 5
236 }
237  
238 10 $arg->{class} = $obj_class;
239 10 ▶ $arg->{object_id} = $id if defined $id and not exists $arg->{object_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and not exists $arg->{"object_id"} 8 2 10
Condition66%2/3
defined $id and not exists $arg->{"object_id"}
!ll&&!rl&&r
208
MC/DC50%1/2
defined $id and not exists $arg->{"object_id"}
defined $id not exists $arg->{"object_id"}
Truth table66%2/3
A and not(B)
A= defined $idB= not exists $arg->{"object_id"}
ABresult
0X0
100
111
240  
241 10 my $class_name_method = "$noun\_class";
242 10 $self->$class_name_method->search(%$arg);
243 }
244 3 }
245  
246 3 '2. see footnote #1';
247  
248 __END__
↑ Summary