aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso/Config/Resource.pm
blob: fc80d414146ed73d24a4f9e179e258bd2cbf5822 (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
package WebCoso::Config::Resource; 
use strict;
use warnings;
use Class::Std;
use Scalar::Util 'weaken';
use List::MoreUtils 'any';
use WebCoso::Config;
use WebCoso::X;
 
{
 
my %sources_of :ATTR( :init_arg<sources> :get<sources>);
my %pipelines_of :ATTR( :init_arg<pipeline> :get<pipeline>);
my %dest_of :ATTR( :init_arg<destination> :get<destination>);
my %collections_of :ATTR( :get<collections_ref> );
 
sub BUILD {
    my ($self,$ident,$args_ref)=@_;
 
    $collections_of{$ident}=[];
 
    WebCoso::Config->add_resource($self);
}
 
sub axes {
    return 'filename';
}
 
sub axis {
    my ($self,$axis_name)=@_;
    if ($axis_name eq 'filename') {
        return @{ $self->get_sources() };
    }
    else {
        return;
    }
}
 
sub datastream {
    my ($self,$axis_name,$axis_value,@rest)=@_;
    if (@rest==0 and $axis_name eq 'filename') {
        if ( grep { $_ eq $axis_value }
                 @{ $self->get_sources() }
             ) {
            return _read_file($axis_value);
        }
    }
    else {
        return;
    }
}
 
sub properties {
    return ();
}
 
sub collections {
    my ($self)=@_;
 
    return @{ $self->get_collections_ref() };
}
 
sub add_coll {
    my ($self$collection)=@_;
 
    return if any { $_ eq $collection } @{ $self->get_collections_ref() };
 
    my $weak_collection = $collection;
    weaken $weak_collection;
 
    push @{ $self->get_collections_ref() }, $weak_collection;
 
    $collection->add_res($self);
 
    return;
}
 
sub _read_file :PRIVATE {
    my ($filename)=@_;
 
    local $/;
    open my $fh,'<:raw',$filename
        or WebCoso::X::OpenError->throw(
            filename => $filename,
            error => $!);
    return scalar <$fh>;
}
 
}
 
1;