summaryrefslogtreecommitdiff
path: root/lib/WebCoso/FileSet.pm
blob: 958a649bc514816d1ad4db83094cff5c532a99db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- mode: perl6 -*- 
use WebCoso::File;
 
class WebCoso::FileSet {
    has %.files;
 
    multi method new($self: :$path!, :$basename!, :$ext!) {
        my @filenames = try {
            CATCH { when X::IO { } }
            $path.IO.dir(
                test => /$basename \. .+? \. $ext/,
            )
        // ();
        my %files = map {
            my $lang = (.basename ~~ /$basename \. (.+?) \. $ext/)[0].Str;
            $lang => WebCoso::File.new(:$lang,path=>$_)
        }, @filenames;
        $self.bless(:%files);
    }
 
    multi method new($self: :@files!) {
        $self.bless(files=>{}).merged-with(@files);
    }
    multi method new($self: :%files-hash!) {
        $self.bless(files=>{}).merged-with(%files-hash);
    }
    multi method new($self: :%files!) {
        $self.bless(files=>%files);
    }
 
    submethod BUILD(:%!files) {}
    
    method langs() {
        return %!files.keys;
    }
 
    method files() {
        return %!files.values;
    }
    method export-hash() {
        my %ret;
        for $.langs -> $lang {
            my $f = $.for-lang($lang);
            if ($f.does(Iterable)) {
                %ret{$lang} = $f.list».path».Str;
            }
            else {
                %ret{$lang} = $f.path.Str;
            }
        }
        return %ret;
    }
    
    method for-lang($lang) {
        return %!files{$lang// ();
    }
 
    multi method set-for-lang($lang,WebCoso::File @files) {
        return %!files{$lang} = @files;
    }
    multi method set-for-lang($lang,WebCoso::File $file) {
        return %!files{$lang} = $file;
    }
 
    multi method set-for-lang($lang,@files) {
        return %!files{$lang} = map {
            $_.isa(WebCoso::File) ?? $_ !! WebCoso::File.new($_)
        },@files;
    }
    multi method set-for-lang($lang,$file) {
        return %!files{$lang} = WebCoso::File.new($file);
    }
 
    multi method merged-with(Any:U $) { return self }
    multi method merged-with(% where !*) { return self }
    multi method merged-with(@ where !*) { return self }
 
    multi method merged-with(WebCoso::File %b where *) {
        my %new-files = %!files;
        %new-files{$_} = %b{$_for %b.keys;
        return self.new(
            files => %new-files,
        );
    }
    multi method merged-with(%b where *) {
        my WebCoso::File %files = map {
            my $lang = $_;
            my $v = %b{$_};
            $lang => (
                $v.isa(WebCoso::File)
                ?? $v
                !! $v.does(Iterable)
                ?? [ map { WebCoso::File.new(:$lang,path=>$_) },$v ]
                !! WebCoso::File.new(:$lang,path=>$v)
            )
        },%b.keys;
        return self.merged-with(%files);
    }
    multi method merged-with(WebCoso::File @b where *) {
        self.merged-with(
            %(map { $_.lang => $_ },@b),
        );
    }
    multi method merged-with(@b where *) {
        my WebCoso::File %files = map {
            $_.isa(WebCoso::File)
            ?? (($_.lang//'') => $_)
            !! ('' => WebCoso::File.new(path=>$_))
        },@b;
        self.merged-with(%files);
    }
    multi method merged-with(WebCoso::FileSet:D $b) {
        self.merged-with($b.files);
    }
}