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;