line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Persist::Base; |
2
|
1
|
|
|
1
|
|
1507
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
3
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
510
|
|
|
1
|
|
|
|
|
28
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1302
|
use EO; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use EO::Class; |
7
|
|
|
|
|
|
|
use EO::System; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use base qw( EO ); |
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
exception Class::Persist::Error extends => 'EO::Error'; |
13
|
|
|
|
|
|
|
exception Class::Persist::Error::New extends => 'EO::Error::New'; |
14
|
|
|
|
|
|
|
exception Class::Persist::Error::TimeOut extends => 'Class::Persist::Error'; |
15
|
|
|
|
|
|
|
exception Class::Persist::Error::InvalidParameters extends => 'Class::Persist::Error'; |
16
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub oid { |
27
|
|
|
|
|
|
|
my $self = shift; |
28
|
|
|
|
|
|
|
return $self->set($Class::Persist::ID_FIELD, shift) if @_; |
29
|
|
|
|
|
|
|
$self->set( $Class::Persist::ID_FIELD, $self->generate_oid ) |
30
|
|
|
|
|
|
|
unless $self->get( $Class::Persist::ID_FIELD ); |
31
|
|
|
|
|
|
|
return $self->get( $Class::Persist::ID_FIELD ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
*set_oid = \&oid; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 mk_accessors |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub mk_accessors { |
41
|
|
|
|
|
|
|
my $class = shift; |
42
|
|
|
|
|
|
|
no strict 'refs'; |
43
|
|
|
|
|
|
|
for my $method (@_) { |
44
|
|
|
|
|
|
|
#next if $class->can($method); # don't overwrite existing methods |
45
|
|
|
|
|
|
|
*{ $class."::".$method } = $class->_accessor($method); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _accessor { |
50
|
|
|
|
|
|
|
my ($class, $field) = @_; |
51
|
|
|
|
|
|
|
return sub { |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
return $self->set($field, @_) if @_; |
54
|
|
|
|
|
|
|
return $self->get($field); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 set( column => value, [ column => value ... ] ) |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub set { |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
while (@_) { |
65
|
|
|
|
|
|
|
my $col = shift; |
66
|
|
|
|
|
|
|
my $value = shift; |
67
|
|
|
|
|
|
|
$self->{$col} = $value; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
return $self; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 get( column ) |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub get { |
77
|
|
|
|
|
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
my $col = shift; |
79
|
|
|
|
|
|
|
die "did you mean 'set'?" if @_; |
80
|
|
|
|
|
|
|
return $self->{$col}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _duplicate_from { |
84
|
|
|
|
|
|
|
my $self = shift; |
85
|
|
|
|
|
|
|
my $source = shift; |
86
|
|
|
|
|
|
|
%$self = (); |
87
|
|
|
|
|
|
|
$self->{$_} = $source->{$_} |
88
|
|
|
|
|
|
|
for (keys(%{ $source })); |
89
|
|
|
|
|
|
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub init { |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
my $params; |
96
|
|
|
|
|
|
|
if (ref( $_[0] )) { |
97
|
|
|
|
|
|
|
$params = $_[0]; |
98
|
|
|
|
|
|
|
} else { |
99
|
|
|
|
|
|
|
throw Class::Persist::Error::InvalidParameters text => "Bad number of parameters" |
100
|
|
|
|
|
|
|
unless (scalar(@_) % 2 == 0); |
101
|
|
|
|
|
|
|
$params = { @_ }; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
if ($params) { |
104
|
|
|
|
|
|
|
my $errors = {}; |
105
|
|
|
|
|
|
|
foreach my $method (keys %$params) { |
106
|
|
|
|
|
|
|
if ( my $can = $self->can($method) ) { |
107
|
|
|
|
|
|
|
next unless defined( $params->{$method} ); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $result = eval { $can->($self, $params->{$method}) }; |
110
|
|
|
|
|
|
|
if (UNIVERSAL::isa($@, "Class::Persist::Error::InvalidParameters")) { |
111
|
|
|
|
|
|
|
$errors->{$method} = $@->text; |
112
|
|
|
|
|
|
|
} elsif ($@) { die $@; } |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if (!$result) { |
115
|
|
|
|
|
|
|
$errors->{$method} ||= "Method $method didn't return a true value"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} else { |
119
|
|
|
|
|
|
|
$errors->{$method} = "Method $method doesn't exist"; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
if (%$errors) { |
123
|
|
|
|
|
|
|
throw Class::Persist::Error::Multiple |
124
|
|
|
|
|
|
|
text => "Error calling init for ".ref($self)." - ".Dumper($errors), |
125
|
|
|
|
|
|
|
errors => $errors; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$self->SUPER::init(@_); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _populate { |
133
|
|
|
|
|
|
|
my $self = shift; |
134
|
|
|
|
|
|
|
my $cols = shift; |
135
|
|
|
|
|
|
|
for (keys(%$cols)) { |
136
|
|
|
|
|
|
|
$self->set($_, $cols->{$_}); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
return $self; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub loadModule { |
142
|
|
|
|
|
|
|
my ($self, $class) = @_; |
143
|
|
|
|
|
|
|
EO::Class->new_with_classname( $class )->load; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub emit { |
147
|
|
|
|
|
|
|
my $class = shift; |
148
|
|
|
|
|
|
|
my $msg = shift; |
149
|
|
|
|
|
|
|
use Data::Dumper; |
150
|
|
|
|
|
|
|
$msg = Dumper($msg) if ref($msg); |
151
|
|
|
|
|
|
|
no warnings qw(uninitialized); |
152
|
|
|
|
|
|
|
EO::System->new->error->print( |
153
|
|
|
|
|
|
|
'['.[caller]->[0].'/'.[caller]->[2].'] '. |
154
|
|
|
|
|
|
|
'['. scalar(localtime()) . '] '. |
155
|
|
|
|
|
|
|
$msg . "\n" |
156
|
|
|
|
|
|
|
); |
157
|
|
|
|
|
|
|
return; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub record { |
162
|
|
|
|
|
|
|
my $self = shift; |
163
|
|
|
|
|
|
|
my $exception = shift; |
164
|
|
|
|
|
|
|
my %param = $_[1] ? @_ : ( text => $_[0] ); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $error = "[$exception] "; |
167
|
|
|
|
|
|
|
$error .= join(' / ', map { "$_ => ".($param{$_}||'') } keys(%param) ); |
168
|
|
|
|
|
|
|
#$self->emit($error); |
169
|
|
|
|
|
|
|
$exception->record(%param); |
170
|
|
|
|
|
|
|
return; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub throw { |
174
|
|
|
|
|
|
|
my $self = shift; |
175
|
|
|
|
|
|
|
my $exception = shift; |
176
|
|
|
|
|
|
|
my %param = @_; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
$self->emit($param{text}); |
179
|
|
|
|
|
|
|
$exception->throw(%param); |
180
|
|
|
|
|
|
|
return; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
__END__ |