line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::DataModel::Meta::Utils; |
2
|
19
|
|
|
19
|
|
133
|
use strict; |
|
19
|
|
|
|
|
54
|
|
|
19
|
|
|
|
|
575
|
|
3
|
19
|
|
|
19
|
|
102
|
use warnings; |
|
19
|
|
|
|
|
41
|
|
|
19
|
|
|
|
|
496
|
|
4
|
|
|
|
|
|
|
|
5
|
19
|
|
|
19
|
|
95
|
use strict; |
|
19
|
|
|
|
|
56
|
|
|
19
|
|
|
|
|
370
|
|
6
|
19
|
|
|
19
|
|
91
|
use warnings; |
|
19
|
|
|
|
|
62
|
|
|
19
|
|
|
|
|
618
|
|
7
|
|
|
|
|
|
|
|
8
|
19
|
|
|
19
|
|
117
|
use Carp; |
|
19
|
|
|
|
|
40
|
|
|
19
|
|
|
|
|
1468
|
|
9
|
19
|
|
|
19
|
|
9323
|
use Module::Load qw/load/; |
|
19
|
|
|
|
|
21639
|
|
|
19
|
|
|
|
|
116
|
|
10
|
19
|
|
|
|
|
1671
|
use Params::Validate qw/validate_with SCALAR ARRAYREF CODEREF |
11
|
19
|
|
|
19
|
|
9373
|
BOOLEAN OBJECT HASHREF/; |
|
19
|
|
|
|
|
141034
|
|
12
|
19
|
|
|
19
|
|
10545
|
use List::MoreUtils qw/any/; |
|
19
|
|
|
|
|
261947
|
|
|
19
|
|
|
|
|
129
|
|
13
|
19
|
|
|
19
|
|
20723
|
use mro qw/c3/; |
|
19
|
|
|
|
|
53
|
|
|
19
|
|
|
|
|
157
|
|
14
|
19
|
|
|
19
|
|
12697
|
use SQL::Abstract::More 1.39; |
|
19
|
|
|
|
|
350139
|
|
|
19
|
|
|
|
|
160
|
|
15
|
19
|
|
|
19
|
|
217879
|
use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)]; |
|
19
|
|
|
|
|
60243
|
|
|
19
|
|
|
|
|
147
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# utility function 'does' imported by hand because not really meant |
18
|
|
|
|
|
|
|
# to be publicly exportable from SQL::Abstract::More |
19
|
19
|
|
|
19
|
|
2478
|
BEGIN {no strict 'refs'; *does = \&SQL::Abstract::More::does;} |
|
19
|
|
|
19
|
|
50
|
|
|
19
|
|
|
|
|
643
|
|
|
19
|
|
|
|
|
444
|
|
20
|
|
|
|
|
|
|
|
21
|
19
|
|
|
19
|
|
128
|
use Exporter qw/import/; |
|
19
|
|
|
|
|
52
|
|
|
19
|
|
|
|
|
3895
|
|
22
|
|
|
|
|
|
|
our @EXPORT = qw/define_class define_method |
23
|
|
|
|
|
|
|
define_readonly_accessors define_abstract_methods |
24
|
|
|
|
|
|
|
does/; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my %seen_class_method; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _check_call_as_class_method { |
33
|
1021
|
|
|
1021
|
|
1692
|
my $first_arg = $_[0]; |
34
|
|
|
|
|
|
|
|
35
|
1021
|
50
|
33
|
|
|
8049
|
if ($first_arg && !ref $first_arg && $first_arg->isa(__PACKAGE__) ) { |
|
|
|
33
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
my $func = (caller(1))[3]; |
37
|
|
|
|
|
|
|
carp "calling $func() as class method is obsolete; import and call as a function" |
38
|
0
|
0
|
|
|
|
0
|
unless $seen_class_method{$func}++; |
39
|
0
|
|
|
|
|
0
|
shift @_; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub define_class { |
46
|
102
|
|
|
102
|
1
|
3506
|
&_check_call_as_class_method; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# check parameters |
49
|
102
|
|
|
|
|
3097
|
my %params = validate_with( |
50
|
|
|
|
|
|
|
params => \@_, |
51
|
|
|
|
|
|
|
spec => { |
52
|
|
|
|
|
|
|
name => {type => SCALAR }, |
53
|
|
|
|
|
|
|
isa => {type => ARRAYREF}, |
54
|
|
|
|
|
|
|
metadm => {isa => 'DBIx::DataModel::Meta'}, |
55
|
|
|
|
|
|
|
}, |
56
|
|
|
|
|
|
|
allow_extra => 0, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# deactivate strict refs because we'll be playing with symbol tables |
60
|
19
|
|
|
19
|
|
150
|
no strict 'refs'; |
|
19
|
|
|
|
|
46
|
|
|
19
|
|
|
|
|
6453
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# make sure that all parents are defined |
63
|
102
|
|
|
|
|
677
|
foreach my $parent (@{$params{isa}}) { |
|
102
|
|
|
|
|
338
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# heuristics to decide if a class is loaded (can't rely on %INC) |
66
|
182
|
|
|
176
|
|
580
|
my $is_class_defined = any {! /::$/} keys %{$parent.'::'}; |
|
176
|
|
|
|
|
501
|
|
|
182
|
|
|
|
|
1471
|
|
67
|
|
|
|
|
|
|
# NOTE : we need to exclude symbols ending with '::' because |
68
|
|
|
|
|
|
|
# "require Foo::Bar::Buz" will define ${Foo::Bar::}{'Buz::'} at |
69
|
|
|
|
|
|
|
# compilation time, even if this statement is never executed. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# try to load parent if needed |
72
|
182
|
100
|
|
|
|
831
|
load $parent unless $is_class_defined; |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# inject parents into @ISA |
76
|
102
|
|
|
|
|
504
|
my $class_isa = $params{name}."::ISA"; |
77
|
102
|
100
|
|
|
|
165
|
not @{$class_isa} or croak "won't overwrite \@$class_isa"; |
|
102
|
|
|
|
|
704
|
|
78
|
100
|
|
|
|
|
198
|
@{$class_isa} = @{$params{isa}}; |
|
100
|
|
|
|
|
2159
|
|
|
100
|
|
|
|
|
247
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# use mro 'c3' in that package |
81
|
100
|
|
|
|
|
823
|
mro::set_mro($params{name}, 'c3'); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# install an accessor to the metaclass object within the package |
84
|
|
|
|
|
|
|
define_method(class => $params{name}, |
85
|
|
|
|
|
|
|
name => 'metadm', |
86
|
3046
|
|
|
3046
|
|
22525
|
body => sub {return $params{metadm}}, |
87
|
100
|
|
|
|
|
600
|
check_override => 0, ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub define_method { |
92
|
835
|
|
|
835
|
1
|
1949
|
&_check_call_as_class_method; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# check parameters |
95
|
835
|
|
|
|
|
21337
|
my %params = validate_with( |
96
|
|
|
|
|
|
|
params => \@_, |
97
|
|
|
|
|
|
|
spec => { |
98
|
|
|
|
|
|
|
class => {type => SCALAR }, |
99
|
|
|
|
|
|
|
name => {type => SCALAR }, |
100
|
|
|
|
|
|
|
body => {type => CODEREF }, |
101
|
|
|
|
|
|
|
check_override => {type => BOOLEAN, default => 1}, |
102
|
|
|
|
|
|
|
}, |
103
|
|
|
|
|
|
|
allow_extra => 0, |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# fully qualified name |
107
|
835
|
|
|
|
|
5901
|
my $full_method_name = $params{class}.'::'.$params{name}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# deactiveate strict refs because we'll be playing with symbol tables |
110
|
19
|
|
|
19
|
|
162
|
no strict 'refs'; |
|
19
|
|
|
|
|
68
|
|
|
19
|
|
|
|
|
7472
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# check if method is already there |
113
|
835
|
50
|
|
|
|
1304
|
not defined(&{$full_method_name}) |
|
835
|
|
|
|
|
4313
|
|
114
|
|
|
|
|
|
|
or croak "method $full_method_name is already defined"; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# check if there is a conflict with an inherited method |
117
|
|
|
|
|
|
|
!$params{check_override} or not $params{class}->can($params{name}) |
118
|
835
|
50
|
66
|
|
|
5996
|
or carp "method $params{name} in $params{class} will be overridden"; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# install the method |
121
|
835
|
|
|
|
|
1489
|
*{$full_method_name} = $params{body}; |
|
835
|
|
|
|
|
4069
|
|
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub define_readonly_accessors { |
126
|
84
|
|
|
84
|
1
|
306
|
&_check_call_as_class_method; |
127
|
|
|
|
|
|
|
|
128
|
84
|
|
|
|
|
381
|
my ($target_class, @accessors) = @_; |
129
|
|
|
|
|
|
|
|
130
|
84
|
|
|
|
|
204
|
foreach my $accessor (@accessors) { |
131
|
|
|
|
|
|
|
define_method( |
132
|
|
|
|
|
|
|
class => $target_class, |
133
|
|
|
|
|
|
|
name => $accessor, |
134
|
4458
|
|
|
4458
|
|
6904
|
body => sub { my $self = shift; |
135
|
4458
|
|
|
|
|
7822
|
my $val = $self->{$accessor}; |
136
|
4458
|
|
|
|
|
9044
|
for (ref $val) { |
137
|
4458
|
100
|
|
|
|
10704
|
/^ARRAY$/ and return @$val; |
138
|
3991
|
100
|
|
|
|
7112
|
/^HASH$/ and return %$val; |
139
|
3932
|
|
|
|
|
14216
|
return $val; # otherwise |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
}, |
142
|
599
|
|
|
|
|
2533
|
); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub define_abstract_methods { |
147
|
25
|
|
|
25
|
0
|
112
|
my ($target_class, @methods) = @_; |
148
|
|
|
|
|
|
|
|
149
|
25
|
|
|
|
|
73
|
foreach my $method (@methods) { |
150
|
|
|
|
|
|
|
define_method( |
151
|
|
|
|
|
|
|
class => $target_class, |
152
|
|
|
|
|
|
|
name => $method, |
153
|
1
|
|
|
1
|
|
22
|
body => sub { my $self = shift; |
154
|
1
|
|
33
|
|
|
5
|
my $subclass = ref $self || $self; |
155
|
1
|
|
|
|
|
11
|
die "$subclass should implement a $method() method, as required by $target_class"; |
156
|
|
|
|
|
|
|
}, |
157
|
43
|
|
|
|
|
258
|
); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
__END__ |