Data-AnyXfer

 view release on metacpan or  search on metacpan

lib/Data/AnyXfer/Elastic/Import/File/Simple.pm  view on Meta::CPAN

is required, or there may be unknown side-effects.

This is mostly intended for use within specialised subclasses
such as L<Data::AnyXfer::Elastic::Import::File::MultiPart>.

=cut

sub content {

    my ( $self, $value, $no_save ) = @_;
    my $storage = $self->storage;

    # if there is a value supplied, this is a 'set' operation
    # completely overwrite the underlying item in storage
    # with the new data
    if ($value) {

        unless (ref $value eq 'ARRAY') {
            croak q/Content body (file->content) must be an ARRAY. /
                . q/Perhaps use 'add' instead./;
        }

        # serialise and set the item in storage
        $storage->set_item(
            $self->item_name,
            $self->format->serialise($value));

        # by default we save / persist the value immediately
        # unless no_save is set
        $storage->save unless $no_save;

        # jump the read iterator to the end and return,
        # this should bail out any futher reads
        $self->reset(1);
        return $value;

    }

    # this is a 'get' operation, get everything
    # until we're exhausted
    my @value_list;
    while ($value = $self->get) {
        push @value_list, $value;
    }
    return \@value_list;
}


sub _fetch_content {

    my ( $self, $from_item ) = @_;
    $from_item ||= $self->item_name;

    # deserialise and return the item from storage
    my $value = $self->storage->get_item($from_item);

    # make sure it deserialises to an array ref
    return $value = [@{$self->format->deserialise($value)}]
        if $value;

    # if we're still alive here
    # we don't have a value, so the item must be new or empty
    # so just return a ref to an empty array
    # (but "touch" the item so it exists if we may need to write)
    unless ($self->storage->read_only) {
        $self->storage->set_item($from_item, $self->format->serialise([]));
    }
    return [];
}


sub _load_content {

    my ( $self, $from_item ) = @_;
    return $self->_content_body($self->_fetch_content($from_item));
}


sub _flush_content_body {

    my $self = $_[0];
    # just set the file content to the current value of _content_body
    # this should trigger a save
    return $self->content($self->_content_body, 1);
}



1;

=head1 COPYRIGHT

This software is copyright (c) 2019, Anthony Lucas.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.626 second using v1.01-cache-2.11-cpan-39bf76dae61 )