Skip to content

Commit

Permalink
[Viper] early prototype ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Masak committed Aug 23, 2009
1 parent 740c711 commit 68c15a6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.in
Expand Up @@ -8,7 +8,7 @@ SOURCES=lib/Routes.pm lib/Routes/Route.pm \
lib/Web/Utils.pm \
lib/Web/Request.pm lib/Web/Response.pm \
lib/Handler/HTTPDaemon.pm \
lib/Astaire.pm
lib/Astaire.pm lib/Viper.pm

PIRS=$(SOURCES:.pm=.pir)

Expand Down
1 change: 1 addition & 0 deletions deps.proto
@@ -1,2 +1,3 @@
# These are the dependencies needed by Web.pm
http-daemon
csv
17 changes: 17 additions & 0 deletions drafts/blog-example
@@ -0,0 +1,17 @@
use v6;
use Viper;

class User is Viper::Base {
}

class Post is Viper::Base {
has $user_id is persisted;
}

class Comment is Viper::Base {
has $user_id is persisted;
}

my $session = Viper.new( :types[User, Post, Comment], :db('data/') );
my Post @posts = Post.find($session, :all);
say .name for @posts;
45 changes: 45 additions & 0 deletions lib/Viper.pm
@@ -0,0 +1,45 @@
use Text::CSV;

class Viper {
has $.db;
has %.objects;

submethod BUILD(:@types!, :$db!) {
$!db = $db;
if $db !~~ :e {
run("mkdir $db");
}
for @types -> $type {
my $filename = $!db ~ '/' ~ $type.substr(0,-2);
if $filename !~~ :e {
self.create-new-db-file($type, $filename);
%!objects{$type} = [];
}
else {
%!objects{$type}
= Text::CSV.parse-file($filename, :output($type));
}
}
}

submethod create-new-db-file($type, $filename) {
my @columns = $type.^attributes>>.name>>.substr(2); # w/o sigil/twigil
my $dbfile = open($filename, :w)
or die $!;
$dbfile.say: join(',', map { quote($_) }, @columns);
}

sub quote($s) { qq["$s"] }
}

class Viper::Base {
has $.id is persisted;
has $.name is persisted;

method find(Viper $session, :$all) {
return $session.objects{self}.list;
}
}

multi trait_mod:<is>(AttributeDeclarand $a, :$persisted!) {
}

0 comments on commit 68c15a6

Please sign in to comment.