Navigation Menu

Skip to content

Commit

Permalink
new & connect methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Sep 3, 2012
1 parent e72636a commit de6f57a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -2,10 +2,11 @@


PERL6 = perl6 PERL6 = perl6
PREFIX = $(HOME)/.perl6 PREFIX = $(HOME)/.perl6
P6LIB = $(PWD)/lib:$(PERL6LIB)
CP = cp -p CP = cp -p
MKDIR = mkdir -p MKDIR = mkdir -p


all: all:


test: all test: all
prove -e '$(PERL6)' -r t/ env PERL6LIB=$(P6LIB) prove -e '$(PERL6)' -r t/
4 changes: 4 additions & 0 deletions TODO
@@ -0,0 +1,4 @@
Commands
========
* quit
* ping
45 changes: 44 additions & 1 deletion lib/Redis.pm
@@ -1,9 +1,52 @@
use v6;

# =begin Pod # =begin Pod
# =head1 Redis # =head1 Redis
# Perl6 binding for Redis. # Perl6 binding for Redis.
# #
# =end Pod # =end Pod


module Redis; class Redis;

has Str $.host = '127.0.0.1';
has Int $.port = 6379;
has Str $.sock; # if sock is defined, use sock
has Bool $.debug = False;
has Real $.timeout = 0.0; # 0 means unlimited
has IO::Socket $.conn;

method new(Str $server?, Bool :$debug?, Real :$timeout?) {
my %config = {}
if $server.defined {
if $server ~~ m/^([\d+]+ %\.) ':' (\d+)$/ {
%config<host> = $0.Str;
%config<port> = $1.Int;
} else {
%config<sock> = $server;
}
}
if $debug.defined {
%config<debug> = $debug;
}
if $timeout.defined {
%config<timeout> = $timeout;
}
return self.bless(*, |%config);
}

method connect {
my $conn;
if $.sock.defined {
say "unsupported";
} else {
$conn = IO::Socket::INET.new(host => $.host, port => $.port);
}
$.conn = $conn;
}

#method ping {
#$.conn.send("PING\r\n");
#print($.conn.get);
#}


# vim: ft=perl6 # vim: ft=perl6
30 changes: 30 additions & 0 deletions t/00-basic.t
@@ -1,3 +1,33 @@
use v6; use v6;


use Redis;
use Test; use Test;

my @new_tasks = \() => {
'host' => '127.0.0.1',
'port' => 6379,
'debug' => False,
'timeout' => 0.0,
},
\('192.168.0.1:6379', debug => True, timeout => 3) => {
'host' => '192.168.0.1',
'port' => 6379,
'debug' => True,
'timeout' => 3,
},
\('/path/to/redis.sock', debug => True, timeout => 3) => {
'sock' => '/path/to/redis.sock',
'debug' => True,
'timeout' => 3,
};

plan [+] @new_tasks.map({ $_.value.elems });

for @new_tasks -> $p {
my $r = Redis.new(|$p.key);
$p.value.map({
is_deeply $r."{.key}"(), .value;
});
}

# vim: ft=perl6
20 changes: 20 additions & 0 deletions t/01-connect.t
@@ -0,0 +1,20 @@
use v6;

use Redis;
use Test;

plan 1;

{
my $r = Redis.new;
my $failed = 0;
try {
$r.connect;
CATCH {
default { $failed = 1 }
}
}
ok $failed, 'trying to connect wrong server throws exception';
}

# vim: ft=perl6

0 comments on commit de6f57a

Please sign in to comment.