Skip to content

Instantly share code, notes, and snippets.

@sdondley
Created June 14, 2022 01:45
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 sdondley/ea3af0b32be1ed08d4f90aeba38179ca to your computer and use it in GitHub Desktop.
Save sdondley/ea3af0b32be1ed08d4f90aeba38179ca to your computer and use it in GitHub Desktop.
use v6.d;
unit module Doc::Examples::Resources;
class Resource {
has Str $.name;
}
class Lesson is Resource {
has IO $.file;
method new(IO:D :$file) {
my $name = $file.basename;
self.bless(:$name, :$file)
}
}
class Topic is Resource {
has Lesson @.lessons;
method new(IO:D :$dir) {
my $files = dir $?DISTRIBUTION.content("$dir");
my $name = $dir.basename;
my @lessons;
for $files.Array -> $file {
push @lessons, Lesson.new(:$file);
}
self.bless(:$name, :@lessons)
}
method lesson-names() {
@.lessons>>.name.sort;
}
method list-lessons() {
self.lesson-names>>.say;
}
}
class LocalResources {
has Topic @.topics;
has Topic %.topic-index;
method new() {
my $dirs = dir $?DISTRIBUTION.content('resources/examples');
my @topics;
my %topic-index;
for $dirs.Array -> $dir {
my $t = Topic.new(:$dir);
push @topics, $t;
%topic-index{$t.name} = $t;
}
self.bless(:@topics, :%topic-index)
}
method list-topics() {
self.topic-names>>.say;
}
method topic-names() {
@.topics>>.name.sort
}
method is-topic(Str:D $topic) {
$topic ~~ any self.topic-names;
}
method get-topic(Str:D $topic) {
if !self.is-topic($topic) {
say "Sorry, that topic does not exist.";
return;
}
return %!topic-index{$topic};
}
method list-lessons(Str:D $topic) {
self.get-topic($topic).list-lessons;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment