Persist/Base.pm

filter: Statementstmt 36.6% 41 / 112 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 11 / 22 Podpod 55.5% 5 / 9 totaltotal 31.4% 57 / 181 CC 23 SCAR 38.3
CC
23
Cov
28%
init
156.0
oid
12.0
record
12.0
CRAP 46.1
↑ Summary Persist/Proxy/Collection.pm »
1 package Class::Persist::Base;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use EO;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use EO::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use EO::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw( EO );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our $VERSION = '0.01';
11  
12 1 exception Class::Persist::Error extends => 'EO::Error';
13 1 exception Class::Persist::Error::New extends => 'EO::Error::New';
14 1 exception Class::Persist::Error::TimeOut extends => 'Class::Persist::Error';
15 1 exception Class::Persist::Error::InvalidParameters extends => 'Class::Persist::Error';
16 1 exception Class::Persist::Error::Multiple extends => 'Class::Persist::Error';
17  
18 =head2 oid
19  
20 A UUID that uniquely identifies this object in the world. It would be bad to
21 change this unless you know what you are doing. It's probably bad even if you
22 do know what you're doing.
23  
24 =cut
25  
26 0 ▶sub oid {
Subroutine oid: not called CC=3 SCAR=24.8
Pod documented
27 0 my $self = shift;
28 0 ▶ return $self->set($Class::Persist::ID_FIELD, shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
29 0 ▶ $self->set( $Class::Persist::ID_FIELD, $self->generate_oid )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get($Class::Persist::ID_FIELD) 0 0 0
30 unless $self->get( $Class::Persist::ID_FIELD );
31 0 return $self->get( $Class::Persist::ID_FIELD );
32 }
33  
34 1 *set_oid = \&oid;
35  
36 =head2 mk_accessors
37  
38 =cut
39  
40 1 ▶sub mk_accessors {
Subroutine mk_accessors: called CC=2 SCAR=6.9
Pod documented
41 1 my $class = shift;
42 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 for my $method (@_) {
44 #next if $class->can($method); # don't overwrite existing methods
45 1 *{ $class."::".$method } = $class->_accessor($method);
46 }
47 }
48  
49 1 ▶sub _accessor {
Subroutine _accessor: called CC=1 SCAR=3.1
50 1 my ($class, $field) = @_;
51 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 my $self = shift;
53 0 ▶ return $self->set($field, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
54 0 return $self->get($field);
55 }
56 1 }
57  
58 =head2 set( column => value, [ column => value ... ] )
59  
60 =cut
61  
62 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
Pod documented
63 0 my $self = shift;
64 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
65 0 my $col = shift;
66 0 my $value = shift;
67 0 $self->{$col} = $value;
68 }
69 0 return $self;
70 }
71  
72 =head2 get( column )
73  
74 =cut
75  
76 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
77 0 my $self = shift;
78 0 my $col = shift;
79 0 ▶ die "did you mean 'set'?" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
80 0 return $self->{$col};
81 }
82  
83 0 ▶sub _duplicate_from {
Subroutine _duplicate_from: not called CC=2 SCAR=17.9
84 0 my $self = shift;
85 0 my $source = shift;
86 0 %$self = ();
87 $self->{$_} = $source->{$_}
88 0 for (keys(%{ $source }));
89 0 return $self;
90 }
91  
92  
93 0 ▶sub init {
Subroutine init: not called CC=12 SCAR=50.5
Pod documented
94 0 my $self = shift;
95 0 my $params;
96 0 ▶ if (ref( $_[0] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
97 0 $params = $_[0];
98 } else {
99 0 ▶ throw Class::Persist::Error::InvalidParameters text => "Bad number of parameters"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(@_) % 2 == 0 0 0 0
100 unless (scalar(@_) % 2 == 0);
101 0 $params = { @_ };
102 }
103 0 ▶ if ($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 0 0 0
104 0 my $errors = {};
105 0 foreach my $method (keys %$params) {
106 0 ▶ if ( my $can = $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $can = $self->can($method)) { } 0 0 0
107 0 ▶ next unless defined( $params->{$method} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$method} 0 0 0
108  
109 0 my $result = eval { $can->($self, $params->{$method}) };
110 0 ▶ if (UNIVERSAL::isa($@, "Class::Persist::Error::InvalidParameters")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($@, 'Class::Persist::Error::InvalidParameters')) { } 0 0 0
elsif ($@) { } 0 0 0
111 0 $errors->{$method} = $@->text;
112 0 } elsif ($@) { die $@; }
113  
114 0 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
115 0 ▶ $errors->{$method} ||= "Method $method didn't return a true value";
Condition0%0/2
$errors->{$method} ||= "Method $method didn't return a true value"
l!l
00
MC/DC0%0/1
$errors->{$method} ||= "Method $method didn't return a true value"
$errors->{$method}
Truth table0%0/2
A
A= $errors->{$method}
Aresult
11
00
116 }
117  
118 } else {
119 0 $errors->{$method} = "Method $method doesn't exist";
120 }
121 }
122 0 ▶ if (%$errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$errors) 0 0 0
123 0 throw Class::Persist::Error::Multiple
124 text => "Error calling init for ".ref($self)." - ".Dumper($errors),
125 errors => $errors;
126 }
127 }
128  
129 0 $self->SUPER::init(@_);
130 }
131  
132 0 ▶sub _populate {
Subroutine _populate: not called CC=2 SCAR=17.9
133 0 my $self = shift;
134 0 my $cols = shift;
135 0 for (keys(%$cols)) {
136 0 $self->set($_, $cols->{$_});
137 }
138 0 return $self;
139 }
140  
141 0 ▶sub loadModule {
Subroutine loadModule: not called CC=1 SCAR=6.9
Pod undocumented
142 0 my ($self, $class) = @_;
143 0 EO::Class->new_with_classname( $class )->load;
144 }
145  
146 0 ▶sub emit {
Subroutine emit: not called CC=2 SCAR=9.6
Pod undocumented
147 0 my $class = shift;
148 0 my $msg = shift;
149 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 0 ▶ $msg = Dumper($msg) if ref($msg);
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg 0 0 0
151 1 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
152 0 EO::System->new->error->print(
153 '['.[caller]->[0].'/'.[caller]->[2].'] '.
154 '['. scalar(localtime()) . '] '.
155 $msg . "\n"
156 );
157 0 return;
158 }
159  
160  
161 0 ▶sub record {
Subroutine record: not called CC=3 SCAR=24.8
Pod undocumented
162 0 my $self = shift;
163 0 my $exception = shift;
164 0 ▶ my %param = $_[1] ? @_ : ( text => $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
165  
166 0 my $error = "[$exception] ";
167 0 ▶ $error .= join(' / ', map { "$_ => ".($param{$_}||'') } keys(%param) );
Condition0%0/2
$param{$_} || ''
l!l
00
MC/DC0%0/1
$param{$_} || ''
$param{$_}
Truth table0%0/2
A
A= $param{$_}
Aresult
11
00
168 #$self->emit($error);
169 0 $exception->record(%param);
170 0 return;
171 }
172  
173 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
174 0 my $self = shift;
175 0 my $exception = shift;
176 0 my %param = @_;
177  
178 0 $self->emit($param{text});
179 0 $exception->throw(%param);
180 0 return;
181 }
182  
183  
184 1 1;
185  
186 __END__
↑ Summary Persist/Proxy/Collection.pm »