diff options
author | dakkar <dakkar@luxion> | 2005-11-05 10:57:58 +0000 |
---|---|---|
committer | dakkar <dakkar@luxion> | 2005-11-05 10:57:58 +0000 |
commit | c8e73a5e1f75b6f6ceff09e088c0f69861b6c448 (patch) | |
tree | e050275e3ad19c0c306a3072c1212b5627222eaf | |
parent | creato contenitore collezioni, migliorata gestione lingue (diff) | |
download | WebCoso-c8e73a5e1f75b6f6ceff09e088c0f69861b6c448.tar.gz WebCoso-c8e73a5e1f75b6f6ceff09e088c0f69861b6c448.tar.bz2 WebCoso-c8e73a5e1f75b6f6ceff09e088c0f69861b6c448.zip |
spostata eccezione di file not found, cambiati i 'die' con Exception::Class, inizio di aggancio c-r
git-svn-id: svn://luxion/repos/WebCoso/trunk@19 fcb26f47-9200-0410-b104-b98ab5b095f3
-rw-r--r-- | lib/WebCoso/Config.pm | 20 | ||||
-rw-r--r-- | lib/WebCoso/Config/Collection.pm | 28 | ||||
-rw-r--r-- | lib/WebCoso/Config/Resource.pm | 33 | ||||
-rw-r--r-- | lib/WebCoso/X.pm | 25 | ||||
-rw-r--r-- | t/01-config.t | 46 |
5 files changed, 130 insertions, 22 deletions
diff --git a/lib/WebCoso/Config.pm b/lib/WebCoso/Config.pm index 9cef6a3..f8dee5a 100644 --- a/lib/WebCoso/Config.pm +++ b/lib/WebCoso/Config.pm @@ -2,6 +2,7 @@ package WebCoso::Config; use strict; use warnings; use WebCoso::Config::Collections; +use WebCoso::X; my @resources; my $collections=WebCoso::Config::Collections->new(); @@ -16,7 +17,9 @@ WebCoso::Config::Helpers->import(); EOF eval $content; if ($@) { - die("Error reading configuration from $filename: $@\n"); + WebCoso::X::ConfigError->throw( + filename => $filename, + error => $@); } else { return 1; @@ -63,12 +66,19 @@ sub res { my $config_dir=file($FILENAME)->parent->absolute; $dest_filename=file($dest_filename)->absolute($config_dir); - for (@source_files) { - $_=file($_)->absolute($config_dir); - } + + my @abs_source_files = map { + my $abs_name=file($_)->absolute($config_dir); + if (!-e $abs_name) { + WebCoso::X::FileNotFound->throw( + filename => $_, + abs_filename => $abs_name); + }; + $abs_name; + } @source_files; WebCoso::Config::Resource->new({ - source=>[@source_files], + sources=>[@abs_source_files], pipeline=>$pipeline_name, destination=>$dest_filename }); diff --git a/lib/WebCoso/Config/Collection.pm b/lib/WebCoso/Config/Collection.pm index c4b6347..6108623 100644 --- a/lib/WebCoso/Config/Collection.pm +++ b/lib/WebCoso/Config/Collection.pm @@ -28,7 +28,11 @@ sub BUILD { $children_of{$ident}=$children; $_->add_parent($self) for @$children; + $resources_of{$ident}=[]; + WebCoso::Config->add_collection($self); + + return; } sub axes { @@ -74,6 +78,8 @@ sub add_child { push @{ $self->get_children_ref() },$child; $child->add_parent($self); + + return; } sub add_parent { my ($self,$parent)=@_; @@ -85,6 +91,28 @@ sub add_parent { push @{ $self->get_parents_ref() },$weak_parent; $parent->add_child($self); + + return; +} +sub add_res { + my ($self, @resources)=@_; + + # creo una tabellina di look-up per evitare i duplicati + # NOTA: le chiavi sono stringhe, non ref, non si può usare per + # pescare gli oggetti + my %res_key; + @res_key{ @{ $self->get_resources_ref() } } = (); + + RESOURCES: + for my $res (@resources) { + next RESOURCES if exists $res_key{$res}; + + push @{ $self->get_resources_ref() }, $res; + $res_key{$res}=undef; + $res->add_coll($self); + } + + return; } sub get_parents { diff --git a/lib/WebCoso/Config/Resource.pm b/lib/WebCoso/Config/Resource.pm index 4b5cd60..fc80d41 100644 --- a/lib/WebCoso/Config/Resource.pm +++ b/lib/WebCoso/Config/Resource.pm @@ -2,16 +2,23 @@ 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<source> :get<sources>); +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); } @@ -48,14 +55,34 @@ sub properties { } sub collections { - return (); + 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 die "Can't open $filename: $!\n"; + open my $fh,'<:raw',$filename + or WebCoso::X::OpenError->throw( + filename => $filename, + error => $!); return scalar <$fh>; } diff --git a/lib/WebCoso/X.pm b/lib/WebCoso/X.pm new file mode 100644 index 0000000..9376d73 --- /dev/null +++ b/lib/WebCoso/X.pm @@ -0,0 +1,25 @@ +package WebCoso::X; +use utf8; + +use Exception::Class + ( + 'WebCoso::X::FileError' => { + fields => [ 'filename' ], + description => 'classe base per errori relativi a file', + }, + 'WebCoso::X::ConfigError' => { + isa => 'WebCoso::X::FileError', + description => 'errore nel file di configurazione', + }, + 'WebCoso::X::FileNotFound' => { + isa => 'WebCoso::X::FileError', + fields => [ 'abs_filename' ], + description => 'un file di una risorsa non è presente', + }, + 'WebCoso::X::OpenError' => { + isa => 'WebCoso::X::FileError', + description => ' non è stato possibile aprire un file', + }, + ); + +1; diff --git a/t/01-config.t b/t/01-config.t index e8b8ee6..d4c8e85 100644 --- a/t/01-config.t +++ b/t/01-config.t @@ -54,20 +54,10 @@ my $conf_file=<<'EOF'; res('src/file1.rest.txt','src/nonce.rest.txt','Id','dst/file.html'); EOF -ok(WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test"), - 'eseguita la configurazione'); - -my @resources=WebCoso::Config->get_all_resources(); -is(scalar @resources,1,'una risorsa definta'); - -is_deeply( - [$resources[0]->axis('filename')], - ["$thisdir/src/file1.rest.txt", "$thisdir/src/nonce.rest.txt"], - 'filenames'); -dies_ok - { - $resources[0]->datastream(filename=>"$thisdir/src/nonce.rest.txt") - } +throws_ok { + WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test") + } + 'WebCoso::X::ConfigError', 'muore se non trova il file'; } @@ -239,3 +229,31 @@ is_deeply( [], 'nessuna foglia'); } + +WebCoso::Config->clear(); + +{ +my $conf_file=<<'EOF'; +$r1=res('src/file1.rest.txt','Id','dst/file.html'); +$c1=coll('coll1'); +$c1->add_res($r1); +EOF + +ok(WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test"), + 'eseguita la configurazione (no strict)'); + +my @resources=WebCoso::Config->get_all_resources(); +my $collections=WebCoso::Config->get_collections(); +my @collections=$collections->get_all_collections(); + +is(scalar @resources,1,'una risorsa'); +is(scalar @collections,1,'una collezione'); +is_deeply( + [$resources[0]->collections()], + [$collections[0]], + 'aggancio r->c'); +is_deeply( + [$collections[0]->get_resources()], + [$resources[0]], + 'aggancio c->r'); +} |