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