| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MongoDBx::Tiny::Util; |
|
2
|
1
|
|
|
1
|
|
4395
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
MongoDBx::Tiny::Util - internal implementation |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Carp qw/carp confess/; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
109
|
|
|
12
|
1
|
|
|
1
|
|
1039
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
13889
|
|
|
|
1
|
|
|
|
|
74
|
|
|
13
|
1
|
|
|
1
|
|
9
|
use Scalar::Util qw(blessed); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
406
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
|
16
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
|
17
|
|
|
|
|
|
|
our @EXPORT = qw/DEBUG |
|
18
|
|
|
|
|
|
|
util_class_is_ours |
|
19
|
|
|
|
|
|
|
util_guess_class |
|
20
|
|
|
|
|
|
|
util_class_attr |
|
21
|
|
|
|
|
|
|
util_document_class |
|
22
|
|
|
|
|
|
|
util_to_oid |
|
23
|
|
|
|
|
|
|
/; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# xxx |
|
26
|
0
|
|
|
0
|
0
|
|
sub DEBUG { $ENV{MONGODBX_TINY_DEBUG} } |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub util_class_is_ours { |
|
29
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
|
30
|
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
return unless $class; |
|
32
|
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
my ($tiny,$doc) = qw(MongoDBx::Tiny MongoDBx::Tiny::Document); |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
return $tiny if eval { $class->isa($tiny) }; |
|
|
0
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
return $doc if eval { $class->isa($doc) }; |
|
|
0
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
return; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub util_guess_class { |
|
41
|
|
|
|
|
|
|
# |
|
42
|
|
|
|
|
|
|
# ($class,$stat) = util_guess_class($proto) |
|
43
|
|
|
|
|
|
|
# |
|
44
|
0
|
|
|
0
|
0
|
|
my $proto = shift; |
|
45
|
0
|
|
0
|
|
|
|
my $caller = shift || (caller(1))[0]; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $class; |
|
48
|
0
|
|
|
|
|
|
my $stat = { |
|
49
|
|
|
|
|
|
|
ours => '', |
|
50
|
|
|
|
|
|
|
object => '', |
|
51
|
|
|
|
|
|
|
caller => '', |
|
52
|
|
|
|
|
|
|
}; |
|
53
|
0
|
0
|
0
|
|
|
|
if (!ref $proto && ($stat->{ours} = util_class_is_ours($proto))) { |
|
|
|
0
|
0
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
$class = $proto; # via classname class->foo |
|
55
|
|
|
|
|
|
|
} elsif (blessed $proto && ($stat->{ours} = util_class_is_ours(ref $proto))) { |
|
56
|
0
|
|
|
|
|
|
$class = ref $proto; # via object |
|
57
|
0
|
|
|
|
|
|
$stat->{object} = ref $proto, |
|
58
|
|
|
|
|
|
|
} else { # direct |
|
59
|
0
|
|
|
|
|
|
$class = $caller; |
|
60
|
0
|
|
|
|
|
|
$stat->{caller} = $caller; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
0
|
|
|
|
|
|
return ($class,$stat); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub util_class_attr { |
|
66
|
0
|
|
|
0
|
0
|
|
my ($attr,$proto,@arg) = @_; |
|
67
|
0
|
0
|
|
|
|
|
confess "no " . $attr unless $attr; |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my ($class,$stat) = util_guess_class($proto,(caller(1))[0]); |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
if ($stat->{caller}) { |
|
72
|
0
|
0
|
|
|
|
|
unshift @arg, $proto if $proto; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $classdata = sprintf "%s::_%s",$class,$attr; |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $val; |
|
78
|
|
|
|
|
|
|
{ |
|
79
|
1
|
|
|
1
|
|
7
|
no strict 'refs'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
89
|
|
|
|
0
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
$val = ${"$classdata"}; |
|
|
0
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
if (@arg) { |
|
84
|
0
|
0
|
|
|
|
|
if (scalar @arg > 1) { |
|
85
|
0
|
|
|
|
|
|
$val = \@arg; |
|
86
|
|
|
|
|
|
|
} else { |
|
87
|
0
|
|
|
|
|
|
$val = $arg[0]; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
{ |
|
90
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
362
|
|
|
|
0
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
${"$classdata"} = $val; |
|
|
0
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return $val; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub util_document_class { |
|
99
|
|
|
|
|
|
|
# guess document class name from (collection name) |
|
100
|
|
|
|
|
|
|
# $d_class = util_document_class($c_name,ref $self); |
|
101
|
0
|
0
|
|
0
|
0
|
|
my $c_name = shift or confess q/no collecion name/; |
|
102
|
0
|
0
|
|
|
|
|
my $base_class = shift or confess q/no base_class/; |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
$c_name = ucfirst $c_name; |
|
105
|
0
|
|
|
|
|
|
$c_name =~ s/_([a-z])/uc($1)/eg; |
|
|
0
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $class = sprintf "%s::%s",$base_class,$c_name; |
|
107
|
0
|
|
|
|
|
|
eval "require $class"; |
|
108
|
0
|
0
|
0
|
|
|
|
if ($@ && ! DEBUG) { |
|
109
|
0
|
|
|
|
|
|
confess "load fail : $class " . $@; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
0
|
|
|
|
|
|
return $class; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub util_to_oid { |
|
115
|
|
|
|
|
|
|
# |
|
116
|
|
|
|
|
|
|
# util_to_oid($document,'_id','foo_id','bar_id') |
|
117
|
|
|
|
|
|
|
# |
|
118
|
0
|
|
|
0
|
0
|
|
my $document = shift; |
|
119
|
0
|
|
|
|
|
|
my @oid_fields = @_; |
|
120
|
0
|
|
|
|
|
|
for (@oid_fields) { |
|
121
|
0
|
0
|
|
|
|
|
if (exists $document->{$_}) { |
|
122
|
0
|
0
|
|
|
|
|
if (ref $document->{$_} eq 'MongoDB::OID') { |
|
|
|
0
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# |
|
124
|
|
|
|
|
|
|
} elsif( $document->{$_} =~ /\A[a-fA-F\d]{24}\z/) { |
|
125
|
0
|
|
|
|
|
|
$document->{$_} = MongoDB::OID->new(value => $document->{$_}); |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
0
|
|
|
|
|
|
return $document; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |
|
133
|
|
|
|
|
|
|
__END__ |