| 1 | package Catmandu::Plugin::Datestamps; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Role::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | before add => sub { | ||||||||||||||||||||
| 12 | 2 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 13 | 2 | my $now = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime(time)); | |||||||||||||||||||
| 14 | 2 | ▶ | $data->{date_created} ||= $now; | ||||||||||||||||||
|
Condition66%2/3
$data->{"date_created"} ||= $now
MC/DC0%0/2
$data->{"date_created"} ||= $now
$data->{"date_created"}
$now
Truth table66%2/3
A ||= B
A= $data->{"date_created"}B= $now
| |||||||||||||||||||||
| 15 | 2 | $data->{date_updated} = $now; | |||||||||||||||||||
| 16 | 1 | }; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | 1; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | __END__ | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =pod | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | Catmandu::Plugin::Datestamps - Automatically add datestamps to Catmandu::Store records | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Using configuration files | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | $ cat catmandu.yml | ||||||||||||||||||||
| 33 | --- | ||||||||||||||||||||
| 34 | store: | ||||||||||||||||||||
| 35 | test: | ||||||||||||||||||||
| 36 | package: MongoDB | ||||||||||||||||||||
| 37 | options: | ||||||||||||||||||||
| 38 | database_name: test | ||||||||||||||||||||
| 39 | bags: | ||||||||||||||||||||
| 40 | data: | ||||||||||||||||||||
| 41 | plugins: | ||||||||||||||||||||
| 42 | - Datestamps | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | $ echo '{"hello":"world"}' | catmandu import JSON to test | ||||||||||||||||||||
| 45 | $ catmandu export test to YAML | ||||||||||||||||||||
| 46 | --- | ||||||||||||||||||||
| 47 | _id: ADA305D8-697D-11E3-B0C3-97AD572FA7E3 | ||||||||||||||||||||
| 48 | date_created: 2013-12-20T13:50:25Z | ||||||||||||||||||||
| 49 | date_updated: 2013-12-20T13:50:25Z | ||||||||||||||||||||
| 50 | hello: world | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # Or in your Perl program | ||||||||||||||||||||
| 53 | my $store = Catmandu::Store::MongoDB->new( | ||||||||||||||||||||
| 54 | database_name => 'test' , | ||||||||||||||||||||
| 55 | bags => { | ||||||||||||||||||||
| 56 | data => { | ||||||||||||||||||||
| 57 | plugins => [qw(Datestamps)] | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | }); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | $store->bag->add({ | ||||||||||||||||||||
| 62 | '_id' => '123', | ||||||||||||||||||||
| 63 | 'name' => 'John Doe' | ||||||||||||||||||||
| 64 | }); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | my $obj = $store->bag->get('123'); | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | print "%s created at %s\n" , $obj->{name} , $obj->{date_created}; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | The Catmandu::Plugin::Datestamps plugin automatically adds/updates datestamp fields in your | ||||||||||||||||||||
| 73 | records. If you add this plugin to your Catmandu::Store configuration then automatically a | ||||||||||||||||||||
| 74 | 'date_created' and 'date_updated' field gets added to newly ingested records. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | The plugin should be set for every bag defined in your Catmandu::Store. In the examples above we've | ||||||||||||||||||||
| 77 | set the plugin to the default bag 'data' that is created in every Catmandu::Store. | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | In Catmandu::Store-s that don't have a dynamic schema (e.g. Solr, DBI) these new date fields should be | ||||||||||||||||||||
| 80 | predefined (e.g by changing the schema.xml or tables fields). | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 SEE ALSO | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | L<Catmandu::Store>, L<Catmandu::Bag> | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||