/usr/share/perl5/Catmandu/Plugin/Datestamps.pm is in libcatmandu-perl 1.0700-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | package Catmandu::Plugin::Datestamps;
use Catmandu::Sane;
our $VERSION = '1.07';
use Catmandu::Util qw(check_string now);
use Moo::Role;
use MooX::Aliases;
use namespace::clean;
has datestamp_format => (is => 'lazy');
has datestamp_created_key => (
is => 'lazy',
isa => \&check_string,
alias => 'datestamp_created_field',
);
has datestamp_updated_key => (
is => 'lazy',
isa => \&check_string,
alias => 'datestamp_updated_field',
);
before add => sub {
my ($self, $data) = @_;
my $now = now($self->datestamp_format);
$data->{$self->datestamp_created_key} ||= $now;
$data->{$self->datestamp_updated_key} = $now;
};
sub _build_datestamp_format {'iso_date_time'}
sub _build_datestamp_created_key {'date_created'}
sub _build_datestamp_updated_key {'date_updated'}
1;
__END__
=pod
=head1 NAME
Catmandu::Plugin::Datestamps - Automatically add datestamps to Catmandu::Store records
=head1 SYNOPSIS
# Using configuration files
$ cat catmandu.yml
---
store:
test:
package: MongoDB
options:
database_name: test
bags:
data:
plugins:
- Datestamps
$ echo '{"hello":"world"}' | catmandu import JSON to test
$ catmandu export test to YAML
---
_id: ADA305D8-697D-11E3-B0C3-97AD572FA7E3
date_created: 2013-12-20T13:50:25Z
date_updated: 2013-12-20T13:50:25Z
hello: world
# Or in your Perl program
my $store = Catmandu::Store::MongoDB->new(
database_name => 'test' ,
bags => {
data => {
plugins => [qw(Datestamps)]
}
});
$store->bag->add({
'_id' => '123',
'name' => 'John Doe'
});
my $obj = $store->bag->get('123');
print "%s created at %s\n" , $obj->{name} , $obj->{date_created};
=head1 DESCRIPTION
The Catmandu::Plugin::Datestamps plugin automatically adds/updates datestamp fields in your
records. If you add this plugin to your Catmandu::Store configuration then automatically a
'date_created' and 'date_updated' field gets added to newly ingested records.
The plugin should be set for every bag defined in your Catmandu::Store. In the examples above we've
set the plugin to the default bag 'data' that is created in every Catmandu::Store.
In Catmandu::Store-s that don't have a dynamic schema (e.g. Solr, DBI) these new date fields should be
predefined (e.g by changing the schema.xml or tables fields).
=head1 CONFIGURATION
=over
=item datestamp_created_key
Field name where the creation date is stored. Defaults to 'date_created'. Also
aliased as C<datestamp_created_field>.
=item datestamp_updated_key
Field name where the update date is stored. Defaults to 'date_updated'. Also
aliased as C<datestamp_updated_field>.
=item datestamp_format
Use a custom C<strftime> format. See L<Catmandu::Util::now> for possible format values.
my $store = Catmandu::Store::MyDB->new(bags => {book => {plugins =>
['Datestamps'], datestamp_format => '%Y/%m/%d'}});
my $store = Catmandu::Store::MyDB->new(bags => {book => {plugins =>
['Datestamps'], datestamp_format => 'iso_date_time_millis'}});
=back
=head1 SEE ALSO
L<Catmandu::Store>, L<Catmandu::Bag>
=cut
|