line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Aniki::Filter::Declare; |
2
|
28
|
|
|
28
|
|
91382
|
use 5.014002; |
|
28
|
|
|
|
|
127
|
|
3
|
|
|
|
|
|
|
|
4
|
28
|
|
|
28
|
|
163
|
use strict; |
|
28
|
|
|
|
|
67
|
|
|
28
|
|
|
|
|
534
|
|
5
|
28
|
|
|
28
|
|
140
|
use warnings; |
|
28
|
|
|
|
|
62
|
|
|
28
|
|
|
|
|
665
|
|
6
|
|
|
|
|
|
|
|
7
|
28
|
|
|
28
|
|
447
|
use Aniki::Filter; |
|
28
|
|
|
|
|
156
|
|
|
28
|
|
|
|
|
2288
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub import { |
10
|
28
|
|
|
28
|
|
268
|
my $class = shift; |
11
|
28
|
|
|
|
|
82
|
my $caller = caller; |
12
|
|
|
|
|
|
|
|
13
|
28
|
|
|
|
|
408
|
my $filter = Aniki::Filter->new; |
14
|
|
|
|
|
|
|
|
15
|
28
|
|
|
28
|
|
171
|
no strict qw/refs/; ## no critic |
|
28
|
|
|
|
|
70
|
|
|
28
|
|
|
|
|
11221
|
|
16
|
28
|
|
|
|
|
85
|
*{"${caller}::table"} = \&_table; |
|
28
|
|
|
|
|
223
|
|
17
|
28
|
|
|
|
|
105
|
*{"${caller}::inflate"} = _inflate($filter); |
|
28
|
|
|
|
|
139
|
|
18
|
28
|
|
|
|
|
101
|
*{"${caller}::deflate"} = _deflate($filter); |
|
28
|
|
|
|
|
152
|
|
19
|
28
|
|
|
|
|
101
|
*{"${caller}::trigger"} = _trigger($filter); |
|
28
|
|
|
|
|
152
|
|
20
|
28
|
|
|
|
|
92
|
*{"${caller}::instance"} = _instance($filter); |
|
28
|
|
|
|
|
4966
|
|
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $TARGET_TABLE; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _table ($&) {## no critic |
26
|
27
|
|
|
27
|
|
357
|
my ($table, $code) = @_; |
27
|
27
|
|
|
|
|
72
|
local $TARGET_TABLE = $table; |
28
|
27
|
|
|
|
|
100
|
$code->(); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _inflate { |
32
|
28
|
|
|
28
|
|
66
|
my $filter = shift; |
33
|
|
|
|
|
|
|
return sub ($&) {## no critic |
34
|
28
|
|
|
28
|
|
183
|
my ($column, $code) = @_; |
35
|
28
|
100
|
|
|
|
112
|
if (defined $TARGET_TABLE) { |
36
|
27
|
|
|
|
|
157
|
$filter->add_table_inflator($TARGET_TABLE, $column, $code); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
1
|
|
|
|
|
5
|
$filter->add_global_inflator($column, $code); |
40
|
|
|
|
|
|
|
} |
41
|
28
|
|
|
|
|
146
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _deflate { |
45
|
28
|
|
|
28
|
|
66
|
my $filter = shift; |
46
|
|
|
|
|
|
|
sub ($&) {## no critic |
47
|
28
|
|
|
28
|
|
194
|
my ($column, $code) = @_; |
48
|
28
|
100
|
|
|
|
91
|
if (defined $TARGET_TABLE) { |
49
|
27
|
|
|
|
|
129
|
$filter->add_table_deflator($TARGET_TABLE, $column, $code); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
1
|
|
|
|
|
4
|
$filter->add_global_deflator($column, $code); |
53
|
|
|
|
|
|
|
} |
54
|
28
|
|
|
|
|
154
|
}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _trigger { |
58
|
28
|
|
|
28
|
|
68
|
my $filter = shift; |
59
|
|
|
|
|
|
|
sub ($&) {## no critic |
60
|
0
|
|
|
0
|
|
0
|
my ($event, $code) = @_; |
61
|
0
|
0
|
|
|
|
0
|
if (defined $TARGET_TABLE) { |
62
|
0
|
|
|
|
|
0
|
$filter->add_table_trigger($TARGET_TABLE, $event, $code); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else { |
65
|
0
|
|
|
|
|
0
|
$filter->add_global_trigger($event, $code); |
66
|
|
|
|
|
|
|
} |
67
|
28
|
|
|
|
|
112
|
}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _instance { |
71
|
28
|
|
|
28
|
|
65
|
my $filter = shift; |
72
|
28
|
|
|
29
|
|
169
|
return sub { $filter }; |
|
29
|
|
|
|
|
98
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |
76
|
|
|
|
|
|
|
__END__ |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=pod |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=encoding utf-8 |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 NAME |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Aniki::Filter::Declare - DSL for declaring actions on sometimes |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 SYNOPSIS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
package MyApp::DB::Filter; |
89
|
|
|
|
|
|
|
use strict; |
90
|
|
|
|
|
|
|
use warnings; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
use Aniki::Filter::Declare; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
use Scalar::Util qw/blessed/; |
95
|
|
|
|
|
|
|
use Time::Moment; |
96
|
|
|
|
|
|
|
use Data::GUID::URLSafe; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# apply callback to row before insert |
99
|
|
|
|
|
|
|
trigger insert => sub { |
100
|
|
|
|
|
|
|
my ($row, $next) = @_; |
101
|
|
|
|
|
|
|
$row->{created_at} = Time::Moment->now; |
102
|
|
|
|
|
|
|
return $next->($row); |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# define trigger/inflate/deflate filters in table context. |
106
|
|
|
|
|
|
|
table author => sub { |
107
|
|
|
|
|
|
|
trigger insert => sub { |
108
|
|
|
|
|
|
|
my ($row, $next) = @_; |
109
|
|
|
|
|
|
|
$row->{guid} = Data::GUID->new->as_base64_urlsafe; |
110
|
|
|
|
|
|
|
return $next->($row); |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
inflate name => sub { |
114
|
|
|
|
|
|
|
my $name = shift; |
115
|
|
|
|
|
|
|
return uc $name; |
116
|
|
|
|
|
|
|
}; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
deflate name => sub { |
119
|
|
|
|
|
|
|
my $name = shift; |
120
|
|
|
|
|
|
|
return lc $name; |
121
|
|
|
|
|
|
|
}; |
122
|
|
|
|
|
|
|
}; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# define inflate/deflate filters in global context. (apply to all tables) |
125
|
|
|
|
|
|
|
inflate qr/_at$/ => sub { |
126
|
|
|
|
|
|
|
my $datetime = shift; |
127
|
|
|
|
|
|
|
return Time::Moment->from_string($datetime.'Z', lenient => 1); |
128
|
|
|
|
|
|
|
}; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
deflate qr/_at$/ => sub { |
131
|
|
|
|
|
|
|
my $datetime = shift; |
132
|
|
|
|
|
|
|
return $datetime->at_utc->strftime('%F %T') if blessed $datetime and $datetime->isa('Time::Moment'); |
133
|
|
|
|
|
|
|
return $datetime; |
134
|
|
|
|
|
|
|
}; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 FUNCTIONS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=over 4 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item C<table> |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item C<inflate> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item C<deflate> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item C<trigger> |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=back |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SEE ALSO |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L<perl> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 LICENSE |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Copyright (C) karupanerura. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
159
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 AUTHOR |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
karupanerura E<lt>karupa@cpan.orgE<gt> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |