Mojo::Asset::Memory - In-Memory Asset


Mojolicious documentation Contained in the Mojolicious distribution.

Index


Code Index:

NAME

Top

Mojo::Asset::Memory - In-Memory Asset

SYNOPSIS

Top

  use Mojo::Asset::Memory;

  my $asset = Mojo::Asset::Memory->new;
  $asset->add_chunk('foo bar baz');
  print $asset->slurp;

DESCRIPTION

Top

Mojo::Asset::Memory is a container for in-memory assets.

METHODS

Top

Mojo::Asset::Memory inherits all methods from Mojo::Asset and implements the following new ones.

new

  my $asset = Mojo::Asset::Memory->new;

Construct a new Mojo::Asset::Memory object.

add_chunk

  $asset = $asset->add_chunk('foo bar baz');

Add chunk of data to asset.

contains

  my $position = $asset->contains('bar');

Check if asset contains a specific string.

get_chunk

  my $chunk = $asset->get_chunk($offset);

Get chunk of data starting from a specific position.

move_to

  $asset = $asset->move_to('/foo/bar/baz.txt');

Move asset data into a specific file.

size

  my $size = $asset->size;

Size of asset data in bytes.

slurp

  my $string = $file->slurp;

Read all asset data at once.

SEE ALSO

Top

Mojolicious, Mojolicious::Guides, http://mojolicio.us.


Mojolicious documentation Contained in the Mojolicious distribution.

package Mojo::Asset::Memory;
use Mojo::Base 'Mojo::Asset';

use Carp 'croak';
use IO::File;

# "There's your giraffe, little girl.
#  I'm a boy.
#  That's the spirit. Never give up."
sub new {
  my $self = shift->SUPER::new(@_);
  $self->{content} = '';
  $self;
}

sub add_chunk {
  my ($self, $chunk) = @_;
  utf8::encode $chunk if utf8::is_utf8 $chunk;
  $self->{content} .= $chunk if defined $chunk;
  $self;
}

sub contains {
  my $self = shift;

  my $start = $self->start_range;
  my $pos = index $self->{content}, shift, $start;
  $pos -= $start if $start && $pos >= 0;
  my $end = $self->end_range;

  return -1 if $end && $pos >= $end;
  $pos;
}

sub get_chunk {
  my ($self, $start) = @_;

  $start += $self->start_range;
  my $size = $ENV{MOJO_CHUNK_SIZE} || 131072;
  if (my $end = $self->end_range) {
    $size = $end + 1 - $start if ($start + $size) > $end;
  }

  substr shift->{content}, $start, $size;
}

sub move_to {
  my ($self, $path) = @_;
  my $file = IO::File->new;
  $file->open("> $path") or croak qq/Can't open file "$path": $!/;
  $file->syswrite($self->{content});
  $self;
}

sub size { length shift->{content} }

sub slurp { shift->{content} }

1;
__END__