Skip to content

Commit 173065c

Browse files
committed
Class redesigned
It now hides all underlying complexity of the CU classes, and avoids user from finding nqp and CU stuff
1 parent 762a9f1 commit 173065c

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
use v6.*;
22
use nqp;
3+
constant default-cache-dir = ".doc-cache";
34

45
class CompUnit::PrecompilationRepository::Document is CompUnit::PrecompilationRepository::Default {
56

7+
my $precomp-store;
68
has Str $.doc-name;
9+
has IO::Path $.doc-path;
710
has Str $!key;
11+
has $!precompiled-pod;
12+
has $!handle;
813

9-
submethod TWEAK {
10-
$!key = key-for($!doc-name)
14+
#| Initializes the class variable that contains the precomp store
15+
sub init( $prefix = default-cache-dir ) is export {
16+
$precomp-store =
17+
CompUnit::PrecompilationStore::File.new(
18+
prefix => $prefix.IO
19+
);
1120
}
1221

13-
method key() { $!key }
22+
method new( $doc-path ) {
23+
init() unless $precomp-store;
24+
my $this-path = IO::Path.new( $doc-path );
25+
fail("Path $this-path not found") if ! $this-path.e;
26+
my $doc-name = $this-path.basename;
27+
self.bless(
28+
doc-path => $this-path,
29+
doc-name => $doc-name,
30+
store => $precomp-store
31+
);
32+
}
33+
34+
submethod TWEAK() {
35+
$!key = key-for( $!doc-name);
36+
self.precompile($!doc-path, $!key, :force );
37+
$!handle = self.load($!key)[0];
38+
$!precompiled-pod = nqp::atkey($!handle.unit,'$=pod')[0];
39+
}
40+
41+
method key() {
42+
$!key
43+
}
44+
45+
method precompiled-pod() { $!precompiled-pod }
1446

1547
#! Provides a key for the document with that particular name
1648
sub key-for( Str $doc-name --> Str ) is export {
17-
nqp::sha1($doc-name);
49+
return nqp::sha1($doc-name);
1850
}
1951

2052
}

t/007-load-precompile.t

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ use File::Directory::Tree;
66
use nqp;
77

88
constant cache-name = "cache";
9-
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix =>
10-
cache-name.IO );
9+
init( cache-name );
1110

1211
for <simple sub/simple> -> $doc-name {
13-
my $precomp = CompUnit::PrecompilationRepository::Document.new(
14-
store => $precomp-store,
15-
doc-name => $doc-name );
12+
my $precomp =
13+
CompUnit::PrecompilationRepository::Document.new(
14+
"t/doctest/$doc-name.pod6");
1615
ok( $precomp.key, "Key precomputed");
1716
like( $precomp.key, /<[0..9 A..F]>+/, "Hexa key is correct");
18-
$precomp.precompile("t/doctest/$doc-name.pod6".IO,
19-
$precomp.key, :force );
20-
my $handle = $precomp.load($precomp.key)[0];
21-
my $precompiled-pod = nqp::atkey($handle.unit,'$=pod')[0];
22-
is-deeply $precompiled-pod, $=pod[0], "Load precompiled pod $doc-name";
17+
is-deeply $precomp.precompiled-pod, $=pod[0], "Load precompiled pod
18+
$doc-name";
2319
}
2420

2521
rmtree(cache-name);

0 commit comments

Comments
 (0)