NAME

Aniki::Filter::Declare - DSL for declaring actions on sometimes

SYNOPSIS

package MyApp::DB::Filter;
use strict;
use warnings;

use Aniki::Filter::Declare;

use Scalar::Util qw/blessed/;
use Time::Moment;
use Data::GUID::URLSafe;

# apply callback to row before insert
trigger insert => sub {
    my ($row, $next) = @_;
    $row->{created_at} = Time::Moment->now;
    return $next->($row);
};

# define trigger/inflate/deflate filters in table context.
table author => sub {
    trigger insert => sub {
        my ($row, $next) = @_;
        $row->{guid} = Data::GUID->new->as_base64_urlsafe;
        return $next->($row);
    };

    inflate name => sub {
        my $name = shift;
        return uc $name;
    };

    deflate name => sub {
        my $name = shift;
        return lc $name;
    };
};

# define inflate/deflate filters in global context. (apply to all tables)
inflate qr/_at$/ => sub {
    my $datetime = shift;
    return Time::Moment->from_string($datetime.'Z', lenient => 1);
};

deflate qr/_at$/ => sub {
    my $datetime = shift;
    return $datetime->at_utc->strftime('%F %T') if blessed $datetime and $datetime->isa('Time::Moment');
    return $datetime;
};

FUNCTIONS

table
inflate
deflate
trigger

SEE ALSO

perl

LICENSE

Copyright (C) karupanerura.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

karupanerura <karupa@cpan.org>