Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Viper] early prototype ORM

  • Loading branch information...
commit 68c15a604a1f487eb2774376079c05fa59622170 1 parent 740c711
@masak authored
Showing with 64 additions and 1 deletion.
  1. +1 −1  Makefile.in
  2. +1 −0  deps.proto
  3. +17 −0 drafts/blog-example
  4. +45 −0 lib/Viper.pm
View
2  Makefile.in
@@ -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)
View
1  deps.proto
@@ -1,2 +1,3 @@
# These are the dependencies needed by Web.pm
http-daemon
+csv
View
17 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;
View
45 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!) {
+}
Please sign in to comment.
Something went wrong with that request. Please try again.