Skip to content

Instantly share code, notes, and snippets.

@FCO
Last active December 2, 2018 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FCO/7d37cc9284a950f3ce6153275c4dddec to your computer and use it in GitHub Desktop.
Save FCO/7d37cc9284a950f3ce6153275c4dddec to your computer and use it in GitHub Desktop.
use lib <lib>;
use Red;
model Person { ... }
model Wishlist {
has UInt $!id is id;
has UInt $!wisher-id is referencing{ Person.id };
has Person $.wisher is relationship{ .wisher-id };
has Str:D $.name is column is required;
has Str $.link is column;
}
model Person is rw {
has UInt $.id is id;
has Str $.name is column;
has Str $.email is column;
has UInt $!pair-id is referencing{ ::?CLASS.^alias.id };
has ::?CLASS $.pair is relationship{ .pair-id };
has Wishlist @.wishes is relationship{ .wisher-id }
method draw(::?CLASS:U:) {
my @people = self.^all.pick: *;
for flat @people.rotor: 2 => -1 -> $p1, $p2 {
$p1.pair = $p2;
$p1.^save;
}
given @people.tail {
.pair = @people.head;
.^save
}
}
}
my $*RED-DB = database "SQLite";
Wishlist.^create-table;
Person.^create-table;
my \fernando = Person.^create: :name<Fernando>, :email<fco@aco.com>;
fernando.wishes.create: :name<Comma>, :link<https://commaide.com>;
fernando.wishes.create: :name("perl6 books"), :link<https://perl6book.com>;
fernando.wishes.create: :name("mac book pro"), :link<https://www.apple.com/shop/buy-mac/macbook-pro/15-inch-space-gray-2.6ghz-6-core-512gb#>;
my \aline = Person.^create: :name<Aline>, :email<aja@aco.com>;
aline.wishes.create: :name("a new closet"), :link<https://i.pinimg.com/474x/02/05/93/020593b34c205792a6a7fd7191333fc6--wardrobe-behind-bed-false-wall-wardrobe.jpg>;
my \fernanda = Person.^create: :name<Fernanda>, :email<faco@aco.com>;
fernanda.wishes.create: :name("mimikyu plush"), :link<https://www.pokemoncenter.com/mimikyu-poké-plush-%28standard-size%29---10-701-02831>;
fernanda.wishes.create: :name("camelia plush"), :link<https://farm9.static.flickr.com/8432/28947786492_80056225f3_b.jpg>;
my \sophia = Person.^create: :name<Sophia>, :email<saco@aco.com>;
sophia.wishes.create: :name("baby alive"), :link<https://www.target.com/p/baby-alive-face-paint-fairy-brunette/-/A-51304817>;
Person.draw;
say "{ .name } -> { .pair.name }\n\tWishlist: { .pair.wishes.map(*.name).join: ", " }" for Person.^all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment