Skip to content

Commit

Permalink
added experimental split method to Mojo::ByteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 23, 2011
1 parent 00e6016 commit 7a37c3d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

1.88 2011-08-23 00:00:00
- Added EXPERIMENTAL split method to Mojo::ByteStream.

1.87 2011-08-23 00:00:00
- Added EXPERIMENTAL app method to Mojo::Command.
- Added EXPERIMENTAL t helper to Mojolicious::Plugin::TagHelpers.
Expand Down
15 changes: 15 additions & 0 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -2,6 +2,7 @@ package Mojo::ByteStream;
use Mojo::Base -base;
use overload '""' => sub { shift->to_string }, fallback => 1;

use Mojo::Collection;
use Mojo::Util;

sub import {
Expand Down Expand Up @@ -162,6 +163,11 @@ sub sha1_sum {

sub size { length shift->{bytestream} }

sub split {
my ($self, $p) = @_;
Mojo::Collection->new(split defined $p ? $p : '', $self->{bytestream});
}

sub to_string { shift->{bytestream} }

sub trim {
Expand Down Expand Up @@ -388,6 +394,15 @@ Note that Perl 5.10 or L<Digest::SHA> are required for C<SHA1> support.
Size of bytestream.
=head2 C<split>
my $collection = $stream->split(',');
Turn bytestream into L<Mojo::Collection>.
Note that this method is EXPERIMENTAL and might change without warning!
$stream->split(',')->join("\n")->say;
=head2 C<to_string>
my $string = $stream->to_string;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -34,7 +34,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Smiling Face With Sunglasses';
our $VERSION = '1.87';
our $VERSION = '1.88';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
11 changes: 10 additions & 1 deletion t/mojo/bytestream.t
Expand Up @@ -10,7 +10,7 @@ use Test::More;

plan skip_all => 'Perl 5.10 or Digest::SHA required for this test!'
unless eval { require Digest::SHA; 1 };
plan tests => 131;
plan tests => 137;

use_ok 'Mojo::Util', 'md5_bytes';
use_ok 'Mojo::ByteStream', 'b';
Expand Down Expand Up @@ -359,6 +359,15 @@ is "$stream", "la\nla la", 'right trimmed result';
$stream = b(" \nla\nla\nla\n ")->trim;
is "$stream", "la\nla\nla", 'right trimmed result';

# split
$stream = b('1,2,3,4,5');
is_deeply [$stream->split(',')->each], [1, 2, 3, 4, 5], 'right elements';
is_deeply [$stream->split(qr/,/)->each], [1, 2, 3, 4, 5], 'right elements';
is_deeply [b('54321')->split->each], [5, 4, 3, 2, 1], 'right elements';
is_deeply [b('')->split->each], [], 'no elements';
is_deeply [b('')->split(',')->each], [], 'no elements';
is_deeply [b('')->split(qr/,/)->each], [], 'no elements';

# say and autojoin
my $buffer = '';
open my $handle, '>', \$buffer;
Expand Down

0 comments on commit 7a37c3d

Please sign in to comment.