| 1 | package Catmandu::Bag; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 14 | ▶ | use Catmandu::Sane; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 14 | our $VERSION = '1.0201_02'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 14 | ▶ | use Catmandu::Util qw(:check is_string require_package); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 14 | ▶ | use Catmandu::IdGenerator::UUID; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 14 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 14 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 14 | with 'Catmandu::Logger'; | |||||||||||||||||||
| 13 | 14 | with 'Catmandu::Pluggable'; | |||||||||||||||||||
| 14 | 14 | with 'Catmandu::Iterable'; | |||||||||||||||||||
| 15 | 14 | with 'Catmandu::Addable'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 14 | requires 'get'; | |||||||||||||||||||
| 18 | 14 | requires 'delete'; | |||||||||||||||||||
| 19 | 14 | requires 'delete_all'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 14 | has store => (is => 'ro', required => 1); | |||||||||||||||||||
| 22 | 14 | has name => (is => 'ro', required => 1); | |||||||||||||||||||
| 23 | 14 | has id_key => (is => 'lazy',); | |||||||||||||||||||
| 24 | has id_generator => ( | ||||||||||||||||||||
| 25 | is => 'lazy', | ||||||||||||||||||||
| 26 | coerce => sub { | ||||||||||||||||||||
| 27 | 4 | ▶ | if (is_string($_[0])) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 1 | require_package($_[0], 'Catmandu::IdGenerator')->new; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | else { | ||||||||||||||||||||
| 31 | 3 | $_[0]; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||
| 34 | 14 | ); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub _build_id_key { | ||||||||||||||||||||
| 37 | 20 | ▶ | $_[0]->store->key_for('id'); | ||||||||||||||||||
|
Subroutine
_build_id_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub _build_id_generator { | ||||||||||||||||||||
| 41 | 3 | ▶ | state $uuid = Catmandu::IdGenerator::UUID->new; | ||||||||||||||||||
|
Subroutine
_build_id_generator: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | before get => sub { | ||||||||||||||||||||
| 45 | 64 | ▶ | check_value($_[1]); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 14 | }; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | before add => sub { | ||||||||||||||||||||
| 49 | 74 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 50 | 74 | check_hash_ref($data); | |||||||||||||||||||
| 51 | 71 | ▶ | check_value($data->{$self->id_key} //= $self->generate_id($data)); | ||||||||||||||||||
|
Condition100%3/3
$$data{$self->id_key} //= $self->generate_id($data)
MC/DC100%2/2
$$data{$self->id_key} //= $self->generate_id($data)
$$data{$self->id_key}
$self->generate_id($data)
Truth table100%3/3
A //= B
A= $$data{$self->id_key}B= $self->generate_id($data)
| |||||||||||||||||||||
| 52 | 14 | }; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | before delete => sub { | ||||||||||||||||||||
| 55 | 6 | ▶ | check_value($_[1]); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 14 | }; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | around delete_all => sub { | ||||||||||||||||||||
| 59 | 8 | ▶ | my ($orig, $self) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | 8 | $orig->($self); | |||||||||||||||||||
| 61 | 8 | return; | |||||||||||||||||||
| 62 | 14 | }; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | sub generate_id { | ||||||||||||||||||||
| 65 | 10 | ▶ | $_[0]->id_generator->generate; | ||||||||||||||||||
|
Subroutine
generate_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub get_or_add { | ||||||||||||||||||||
| 69 | 1 | ▶ | my ($self, $id, $data) = @_; | ||||||||||||||||||
|
Subroutine
get_or_add: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||
| 70 | 1 | check_value($id); | |||||||||||||||||||
| 71 | 1 | check_hash_ref($data); | |||||||||||||||||||
| 72 | 1 | ▶ | $self->get($id) || do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | $data->{$self->id_key} = $id; | |||||||||||||||||||
| 74 | 0 | $self->add($data); | |||||||||||||||||||
| 75 | }; | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub to_hash { | ||||||||||||||||||||
| 79 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
to_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | $self->reduce( | ||||||||||||||||||||
| 81 | {}, | ||||||||||||||||||||
| 82 | sub { | ||||||||||||||||||||
| 83 | 1 | ▶ | my ($hash, $data) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | 1 | $hash->{$data->{$self->id_key}} = $data; | |||||||||||||||||||
| 85 | 1 | $hash; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 1 | ); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 14 | 1; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =pod | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head1 NAME | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Catmandu::Bag - A Catmandu::Store compartment to persist data | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | my $store = Catmandu::Store::DBI->new(data_source => 'DBI:mysql:database=test'); | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | my $store = Catmandu::Store::DBI->new( | ||||||||||||||||||||
| 105 | data_source => 'DBI:mysql:database=test', | ||||||||||||||||||||
| 106 | bags => { journals => { | ||||||||||||||||||||
| 107 | fixes => [ ... ] , | ||||||||||||||||||||
| 108 | autocommit => 1 , | ||||||||||||||||||||
| 109 | plugins => [ ... ] , | ||||||||||||||||||||
| 110 | id_generator => Catmandu::IdGenerator::UUID->new , | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | }, | ||||||||||||||||||||
| 113 | bag_class => Catmandu::Bag->with_plugins('Datestamps') | ||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # Use the default bag... | ||||||||||||||||||||
| 117 | my $bag = $store->bag; | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | # Or a named bag... | ||||||||||||||||||||
| 120 | my $bag = $store->bag('journals'); | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # Every bag is an iterator... | ||||||||||||||||||||
| 123 | $bag->each(sub { ... }); | ||||||||||||||||||||
| 124 | $bag->take(10)->each(sub { ... }); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | $bag->add($hash); | ||||||||||||||||||||
| 127 | $bag->add_many($iterator); | ||||||||||||||||||||
| 128 | $bag->add_many([ $hash, $hash , ...]); | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | # Commit changes... | ||||||||||||||||||||
| 131 | $bag->commit; | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | my $obj = $bag->get($id); | ||||||||||||||||||||
| 134 | $bag->delete($id); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | $bag->delete_all; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head1 CONFIGURATION | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =over | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item fixes | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | An array of fixes to apply before importing or exporting data from the bag. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =item plugins | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | An array of Catmandu::Pluggable to apply to the bag items. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =item autocommit | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | When set to a true value an commit automatically gets executed when the bag | ||||||||||||||||||||
| 153 | goes out of scope. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =item id_generator | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | A L<Catmandu::IdGenerator> or name of an IdGenerator class. | ||||||||||||||||||||
| 158 | By default L<Catmandu::IdGenerator::UUID> is used. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =item id_key | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Use a custom key to hold id's in this bag. See L<Catmandu::Store> for the | ||||||||||||||||||||
| 163 | default or store wide value. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =back | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head1 METHODS | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head2 add($hash) | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Add a hash to the bag or updates an existing hash by using its '_id' key. Returns | ||||||||||||||||||||
| 172 | the stored hash on success or undef on failure. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 add_many($array) | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head2 add_many($iterator) | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Add or update one or more items to the bag. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head2 get($id) | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Retrieves the item with identifier $id from the bag. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head2 get_or_add($id, $hash) | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Retrieves the item with identifier $id from the store or adds C<$hash> with _id | ||||||||||||||||||||
| 187 | C<$id> if it's not found. | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head2 delete($id) | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Deletes the item with C<$id> from the bag. | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =head2 delete_all | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | Clear the bag. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head2 commit | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | Commit changes. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head2 log | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | Return the current logger. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head1 CLASS METHODS | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head2 with_plugins($plugin) | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head2 with_plugins(\@plugins) | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Plugins are a kind of fixes that should be available for each bag. E.g. the Datestamps plugin will | ||||||||||||||||||||
| 212 | automatically store into each bag item the fields 'date_updated' and 'date_created'. The with_plugins | ||||||||||||||||||||
| 213 | accept one or an array of plugin classnames and returns a subclass of the Bag with the plugin | ||||||||||||||||||||
| 214 | methods implemented. | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =head1 SEE ALSO | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | L<Catmandu::Iterable>, L<Catmandu::Searchable>, L<Catmandu::Fix>, L<Catmandu::Pluggable> | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||