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