Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new & connect methods

  • Loading branch information...
commit de6f57a656adc0697bbb8dad07cdb1e93c821b26 1 parent e72636a
@Cofyc authored
Showing with 100 additions and 2 deletions.
  1. +2 −1  Makefile
  2. +4 −0 TODO
  3. +44 −1 lib/Redis.pm
  4. +30 −0 t/00-basic.t
  5. +20 −0 t/01-connect.t
View
3  Makefile
@@ -2,10 +2,11 @@
PERL6 = perl6
PREFIX = $(HOME)/.perl6
+P6LIB = $(PWD)/lib:$(PERL6LIB)
CP = cp -p
MKDIR = mkdir -p
all:
test: all
- prove -e '$(PERL6)' -r t/
+ env PERL6LIB=$(P6LIB) prove -e '$(PERL6)' -r t/
View
4 TODO
@@ -0,0 +1,4 @@
+Commands
+========
+ * quit
+ * ping
View
45 lib/Redis.pm
@@ -1,9 +1,52 @@
+use v6;
+
# =begin Pod
# =head1 Redis
# Perl6 binding for Redis.
#
# =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
View
30 t/00-basic.t
@@ -1,3 +1,33 @@
use v6;
+use Redis;
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
View
20 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
Please sign in to comment.
Something went wrong with that request. Please try again.