File Coverage

blib/lib/Catmandu/Plugin/Datestamps.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 26 26 100.0


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