File Coverage

blib/lib/Aniki/Filter/Declare.pm
Criterion Covered Total %
statement 47 51 92.1
branch 4 6 66.6
condition n/a
subroutine 14 15 93.3
pod n/a
total 65 72 90.2


line stmt bran cond sub pod time code
1             package Aniki::Filter::Declare;
2 28     28   103451 use 5.014002;
  28         113  
3              
4 28     28   150 use strict;
  28         62  
  28         490  
5 28     28   136 use warnings;
  28         56  
  28         821  
6              
7 28     28   617 use Aniki::Filter;
  28         157  
  28         1814  
8              
9             sub import {
10 28     28   248 my $class = shift;
11 28         81 my $caller = caller;
12              
13 28         303 my $filter = Aniki::Filter->new;
14              
15 28     28   152 no strict qw/refs/; ## no critic
  28         64  
  28         9925  
16 28         81 *{"${caller}::table"} = \&_table;
  28         179  
17 28         91 *{"${caller}::inflate"} = _inflate($filter);
  28         124  
18 28         79 *{"${caller}::deflate"} = _deflate($filter);
  28         122  
19 28         72 *{"${caller}::trigger"} = _trigger($filter);
  28         116  
20 28         83 *{"${caller}::instance"} = _instance($filter);
  28         4477  
21             }
22              
23             our $TARGET_TABLE;
24              
25             sub _table ($&) {## no critic
26 27     27   336 my ($table, $code) = @_;
27 27         63 local $TARGET_TABLE = $table;
28 27         93 $code->();
29             }
30              
31             sub _inflate {
32 28     28   58 my $filter = shift;
33             return sub ($&) {## no critic
34 28     28   153 my ($column, $code) = @_;
35 28 100       93 if (defined $TARGET_TABLE) {
36 27         135 $filter->add_table_inflator($TARGET_TABLE, $column, $code);
37             }
38             else {
39 1         5 $filter->add_global_inflator($column, $code);
40             }
41 28         173 };
42             }
43              
44             sub _deflate {
45 28     28   56 my $filter = shift;
46             sub ($&) {## no critic
47 28     28   176 my ($column, $code) = @_;
48 28 100       91 if (defined $TARGET_TABLE) {
49 27         108 $filter->add_table_deflator($TARGET_TABLE, $column, $code);
50             }
51             else {
52 1         5 $filter->add_global_deflator($column, $code);
53             }
54 28         97 };
55             }
56              
57             sub _trigger {
58 28     28   55 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         97 };
68             }
69              
70             sub _instance {
71 28     28   51 my $filter = shift;
72 28     29   98 return sub { $filter };
  29         92  
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