Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carl Masak
committed
Aug 23, 2009
1 parent
740c711
commit 68c15a6
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# These are the dependencies needed by Web.pm | ||
http-daemon | ||
csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!) { | ||
} |