| 1 | package Catmandu::Fix::add_to_store; | ||||||||||
| 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 Catmandu; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | has path => (fix_arg => 1); | |||||||||
| 13 | 1 | has store_name => (fix_arg => 1); | |||||||||
| 14 | 1 | has bag_name => (fix_opt => 1, init_arg => 'bag'); | |||||||||
| 15 | 1 | has store_args => (fix_opt => 'collect'); | |||||||||
| 16 | 1 | has store => (is => 'lazy', init_arg => undef); | |||||||||
| 17 | 1 | has bag => (is => 'lazy', init_arg => undef); | |||||||||
| 18 | |||||||||||
| 19 | 1 | with 'Catmandu::Fix::SimpleGetValue'; | |||||||||
| 20 | |||||||||||
| 21 | sub _build_store { | ||||||||||
| 22 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_store: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | Catmandu->store($self->store_name, %{$self->store_args}); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub _build_bag { | ||||||||||
| 27 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_bag: called CC=2 SCAR=7.2
| |||||||||||
| 28 | 1 | ▶ | defined $self->bag_name | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | ? $self->store->bag($self->bag_name) | ||||||||||
| 30 | : $self->store->bag; | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | sub emit_value { | ||||||||||
| 34 | 1 | ▶ | my ($self, $var, $fixer) = @_; | ||||||||
|
Subroutine
emit_value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | 1 | my $bag_var = $fixer->capture($self->bag); | |||||||||
| 36 | |||||||||||
| 37 | 1 | "if (is_hash_ref(${var})) {" . "${bag_var}->add(${var});" . "}"; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | 1; | |||||||||
| 41 | |||||||||||
| 42 | __END__ | ||||||||||
| 43 | |||||||||||
| 44 | =pod | ||||||||||
| 45 | |||||||||||
| 46 | =head1 NAME | ||||||||||
| 47 | |||||||||||
| 48 | Catmandu::Fix::add_to_store - add matching values to a store as a side effect | ||||||||||
| 49 | |||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||
| 51 | |||||||||||
| 52 | # Add the current record to an SQLLite database. | ||||||||||
| 53 | add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") | ||||||||||
| 54 | |||||||||||
| 55 | # Add the journal field to a MongoDB database. | ||||||||||
| 56 | add_to_store(journal, MongoDB, database_name: catalog) | ||||||||||
| 57 | |||||||||||
| 58 | # Add all author values to a MongoDB database. | ||||||||||
| 59 | add_to_store(authors.*, MongoDB, database_name: catalog, bag: authors) | ||||||||||
| 60 | |||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||
| 62 | |||||||||||
| 63 | =head2 add_to_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name]) | ||||||||||
| 64 | |||||||||||
| 65 | Store a record or parts of a record in a Catmandu::Store. | ||||||||||
| 66 | The values at the PATH will be stored as-is in the database but should be hashes. | ||||||||||
| 67 | If the value contains an '_id' field, then it will | ||||||||||
| 68 | used as record identifier in the database. If not, then a new '_id' field will | ||||||||||
| 69 | be generated and added to the database and original field (for later reference). | ||||||||||
| 70 | |||||||||||
| 71 | For instance this YAML input: | ||||||||||
| 72 | |||||||||||
| 73 | --- | ||||||||||
| 74 | _id: 001 | ||||||||||
| 75 | title: test | ||||||||||
| 76 | name: foo | ||||||||||
| 77 | --- | ||||||||||
| 78 | _id: 002 | ||||||||||
| 79 | title: test2 | ||||||||||
| 80 | name: bar | ||||||||||
| 81 | |||||||||||
| 82 | with the fix: | ||||||||||
| 83 | |||||||||||
| 84 | add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") | ||||||||||
| 85 | |||||||||||
| 86 | will create a path/data.sqlite SQLite database with two records. Each records contains | ||||||||||
| 87 | the _id from the input file and all the record fields. | ||||||||||
| 88 | |||||||||||
| 89 | For instance this YAML input: | ||||||||||
| 90 | |||||||||||
| 91 | --- | ||||||||||
| 92 | title: test | ||||||||||
| 93 | name: foo | ||||||||||
| 94 | --- | ||||||||||
| 95 | title: test2 | ||||||||||
| 96 | name: bar | ||||||||||
| 97 | |||||||||||
| 98 | with the fix: | ||||||||||
| 99 | |||||||||||
| 100 | add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") | ||||||||||
| 101 | |||||||||||
| 102 | will create a path/data.sqlite SQLite database with two records. Each records contains | ||||||||||
| 103 | the a generated _id and all the record fields. The current input stream will be updated | ||||||||||
| 104 | to contain the generated _id. | ||||||||||
| 105 | |||||||||||
| 106 | Use L<Catmandu::Fix::lookup_in_store> to lookup records in a Catmandu::Store based | ||||||||||
| 107 | on an '_id' key. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 SEE ALSO | ||||||||||
| 110 | |||||||||||
| 111 | L<Catmandu::Fix> , L<Catmandu::Fix::lookup_in_store> | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | 1; | ||||||||||