line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MongoDBx::Tiny::Util; |
2
|
3
|
|
|
3
|
|
12
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
73
|
|
3
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
75
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
MongoDBx::Tiny::Util - internal implementation |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
10
|
use Carp qw/carp confess/; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
127
|
|
12
|
3
|
|
|
3
|
|
13
|
use Data::Dumper; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
94
|
|
13
|
3
|
|
|
3
|
|
10
|
use Scalar::Util qw(blessed); |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
866
|
|
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
|
0
|
sub DEBUG { $ENV{MONGODBX_TINY_DEBUG} } |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub util_class_is_ours { |
29
|
3
|
|
|
3
|
0
|
4
|
my $class = shift; |
30
|
|
|
|
|
|
|
|
31
|
3
|
50
|
|
|
|
5
|
return unless $class; |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
3
|
my ($tiny,$doc) = qw(MongoDBx::Tiny MongoDBx::Tiny::Document); |
34
|
|
|
|
|
|
|
|
35
|
3
|
100
|
|
|
|
2
|
return $tiny if eval { $class->isa($tiny) }; |
|
3
|
|
|
|
|
23
|
|
36
|
2
|
50
|
|
|
|
2
|
return $doc if eval { $class->isa($doc) }; |
|
2
|
|
|
|
|
8
|
|
37
|
2
|
|
|
|
|
13
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub util_guess_class { |
41
|
|
|
|
|
|
|
# |
42
|
|
|
|
|
|
|
# ($class,$stat) = util_guess_class($proto) |
43
|
|
|
|
|
|
|
# |
44
|
3
|
|
|
3
|
0
|
3
|
my $proto = shift; |
45
|
3
|
|
33
|
|
|
17
|
my $caller = shift || (caller(1))[0]; |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
3
|
my $class; |
48
|
3
|
|
|
|
|
8
|
my $stat = { |
49
|
|
|
|
|
|
|
ours => '', |
50
|
|
|
|
|
|
|
object => '', |
51
|
|
|
|
|
|
|
caller => '', |
52
|
|
|
|
|
|
|
}; |
53
|
3
|
50
|
66
|
|
|
19
|
if (!ref $proto && ($stat->{ours} = util_class_is_ours($proto))) { |
|
|
100
|
66
|
|
|
|
|
54
|
0
|
|
|
|
|
0
|
$class = $proto; # via classname class->foo |
55
|
|
|
|
|
|
|
} elsif (blessed $proto && ($stat->{ours} = util_class_is_ours(ref $proto))) { |
56
|
1
|
|
|
|
|
2
|
$class = ref $proto; # via object |
57
|
1
|
|
|
|
|
2
|
$stat->{object} = ref $proto, |
58
|
|
|
|
|
|
|
} else { # direct |
59
|
2
|
|
|
|
|
2
|
$class = $caller; |
60
|
2
|
|
|
|
|
2
|
$stat->{caller} = $caller; |
61
|
|
|
|
|
|
|
} |
62
|
3
|
|
|
|
|
6
|
return ($class,$stat); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub util_class_attr { |
66
|
3
|
|
|
3
|
0
|
6
|
my ($attr,$proto,@arg) = @_; |
67
|
3
|
50
|
|
|
|
7
|
confess "no " . $attr unless $attr; |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
22
|
my ($class,$stat) = util_guess_class($proto,(caller(1))[0]); |
70
|
|
|
|
|
|
|
|
71
|
3
|
100
|
|
|
|
9
|
if ($stat->{caller}) { |
72
|
2
|
50
|
|
|
|
4
|
unshift @arg, $proto if $proto; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
11
|
my $classdata = sprintf "%s::_%s",$class,$attr; |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
2
|
my $val; |
78
|
|
|
|
|
|
|
{ |
79
|
3
|
|
|
3
|
|
11
|
no strict 'refs'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
181
|
|
|
3
|
|
|
|
|
3
|
|
80
|
3
|
|
|
|
|
2
|
$val = ${"$classdata"}; |
|
3
|
|
|
|
|
9
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
3
|
100
|
|
|
|
6
|
if (@arg) { |
84
|
2
|
100
|
|
|
|
3
|
if (scalar @arg > 1) { |
85
|
1
|
|
|
|
|
1
|
$val = \@arg; |
86
|
|
|
|
|
|
|
} else { |
87
|
1
|
|
|
|
|
2
|
$val = $arg[0]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
{ |
90
|
3
|
|
|
3
|
|
10
|
no strict 'refs'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
784
|
|
|
2
|
|
|
|
|
2
|
|
91
|
2
|
|
|
|
|
4
|
${"$classdata"} = $val; |
|
2
|
|
|
|
|
3
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
3
|
|
|
|
|
19
|
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__ |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 AUTHOR |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Naoto ISHIKAWA, C<< <toona at seesaa.co.jp> >> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Copyright 2013 Naoto ISHIKAWA. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
144
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
145
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |