diff options
-rw-r--r-- | lib/WebCoso/Config.pm | 11 | ||||
-rw-r--r-- | lib/WebCoso/Config/Collection.pm | 20 | ||||
-rw-r--r-- | lib/WebCoso/Config/Collections.pm | 74 | ||||
-rw-r--r-- | t/01-config.t | 53 |
4 files changed, 143 insertions, 15 deletions
diff --git a/lib/WebCoso/Config.pm b/lib/WebCoso/Config.pm index 3fbc597..9cef6a3 100644 --- a/lib/WebCoso/Config.pm +++ b/lib/WebCoso/Config.pm @@ -1,9 +1,10 @@ package WebCoso::Config; use strict; use warnings; +use WebCoso::Config::Collections; my @resources; -my @collections; +my $collections=WebCoso::Config::Collections->new(); sub read_scalar { my ($class,$content,$filename)=@_; @@ -33,16 +34,16 @@ sub get_all_resources { sub add_collection { my ($class,$collection)=@_; - push @collections,$collection; + $collections->add_collection($collection); } -sub get_all_collections { - return @collections; +sub get_collections { + return $collections; } sub clear { @resources=(); - @collections=(); + $collections=WebCoso::Config::Collections->new(); } package WebCoso::Config::Helpers; diff --git a/lib/WebCoso/Config/Collection.pm b/lib/WebCoso/Config/Collection.pm index 367a53e..c4b6347 100644 --- a/lib/WebCoso/Config/Collection.pm +++ b/lib/WebCoso/Config/Collection.pm @@ -2,6 +2,8 @@ package WebCoso::Config::Collection; use strict; use warnings; use Class::Std; +use Scalar::Util 'weaken'; +use List::MoreUtils 'any'; use WebCoso::Config; { @@ -36,7 +38,7 @@ sub axes { sub axis { my ($self,$axis_name)=@_; if ($axis_name eq 'language') { - return keys %{ $self->get_names() } + return grep { $_ } keys %{ $self->get_names() } } else { return; @@ -47,13 +49,18 @@ sub properties { my ($self,$axis_name,$axis_value,@rest)=@_; if (@rest==0 and $axis_name eq 'language') { - if ( grep { $_ eq $axis_value } + if ( any { $_ eq $axis_value } keys %{ $self->get_names() } ) { return { name => $self->get_names()->{$axis_value} }; } + elsif (exists ${$self->get_names()}{''}) { + return { + name => $self->get_names()->{''} + }; + } else { return; } @@ -63,7 +70,7 @@ sub properties { sub add_child { my ($self,$child)=@_; - return if grep { $_ eq $child } @{ $self->get_children_ref() }; + return if any { $_ eq $child } @{ $self->get_children_ref() }; push @{ $self->get_children_ref() },$child; $child->add_parent($self); @@ -71,9 +78,12 @@ sub add_child { sub add_parent { my ($self,$parent)=@_; - return if grep { $_ eq $parent } @{ $self->get_parents_ref() }; + return if any { $_ eq $parent } @{ $self->get_parents_ref() }; + + my $weak_parent=$parent; + weaken $weak_parent; - push @{ $self->get_parents_ref() },$parent; + push @{ $self->get_parents_ref() },$weak_parent; $parent->add_child($self); } diff --git a/lib/WebCoso/Config/Collections.pm b/lib/WebCoso/Config/Collections.pm new file mode 100644 index 0000000..7d4d9e0 --- /dev/null +++ b/lib/WebCoso/Config/Collections.pm @@ -0,0 +1,74 @@ +package WebCoso::Config::Collections; +use strict; +use warnings; +use Class::Std; +use List::MoreUtils 'any'; + +{ +my %collections_of :ATTR( :get<collections_ref> ); + +sub BUILD { + my ($self,$ident,$args_ref)=@_; + + $collections_of{$ident} = []; +} + +sub add_collection { + my ($self, $collection)=@_; + + return if any { $_ eq $collection } $self->get_all_collections(); + + push @{ $self->get_collections_ref() }, $collection; +} + +sub get_all_collections { + my ($self)=@_; + return @{ $self->get_collections_ref() }; +} + +sub get_root_collections { + my ($self)=@_; + + return grep { + $_->get_parents() == 0 + } $self->get_all_collections(); +} + +sub get_leaf_collections { + my ($self)=@_; + + return grep { + $_->get_children() == 0 + } $self->get_all_collections(); +} + +sub axes { + return 'language'; +} + +sub axis { + my ($self,$axis_name)=@_; + if ($axis_name eq 'language') { + return $self->_get_languages(); + } + else { + return; + } +} + +sub _get_languages { + my ($self)=@_; + my %langs=(); + + for my $collection ($self->get_all_collections()) { + @langs{ $collection->axis('language') } = (); + } + + delete $langs{''}; + + return keys %langs; +} + +} + +1; diff --git a/t/01-config.t b/t/01-config.t index 78000a3..e8b8ee6 100644 --- a/t/01-config.t +++ b/t/01-config.t @@ -83,7 +83,9 @@ ok(WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test"), is(scalar WebCoso::Config->get_all_resources(),0,'nessuna risorsa'); -my @collections=WebCoso::Config->get_all_collections(); +my $collections=WebCoso::Config->get_collections(); +isa_ok($collections,'WebCoso::Config::Collections'); +my @collections=$collections->get_all_collections(); is(scalar @collections,1,'una collezione'); is_deeply( @@ -92,8 +94,16 @@ is_deeply( 'multilingua'); is_deeply( [$collections[0]->axis('language')], - [''], + [], 'ma nessuna definita'); +is_deeply( + [$collections->axes()], + ['language'], + 'multilingua, insieme'); +is_deeply( + [$collections->axis('language')], + [], + 'ma nessuna definita, insieme'); my $props=$collections[0]->properties(language=>''); isa_ok($props,'HASH'); is_deeply( @@ -113,6 +123,14 @@ is_deeply( [$collections[0]->get_resources()], [], 'no resources'); +is_deeply( + [$collections->get_root_collections()], + [@collections], + 'una radice'); +is_deeply( + [$collections->get_leaf_collections()], + [@collections], + 'una foglia'); } WebCoso::Config->clear(); @@ -126,7 +144,8 @@ EOF ok(WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test"), 'eseguita la configurazione'); -my @collections=WebCoso::Config->get_all_collections(); +my $collections=WebCoso::Config->get_collections(); +my @collections=$collections->get_all_collections(); is(scalar @collections,2,'due collezioni'); # qui sto assumendo che le collezioni vengano registrate in ordine di @@ -136,6 +155,10 @@ is_deeply( [sort $collections[0]->axis('language')], ['en', 'it'], 'due lingue'); +is_deeply( + [sort $collections->axis('language')], + ['en', 'it'], + 'due lingue'); is( $collections[0]->properties(language=>'en')->{name}, 'coll1-en', @@ -144,6 +167,10 @@ is( $collections[0]->properties(language=>'it')->{name}, 'coll1', 'nome it'); +is( + $collections[1]->properties(language=>'it')->{name}, + 'coll2', + 'nome default'); is_deeply( [$collections[0]->get_parents()], @@ -161,7 +188,14 @@ is_deeply( [$collections[1]->get_children()], [$collections[0]], 'c1 figlio di c2'); - +is_deeply( + [$collections->get_root_collections()], + [$collections[1]], + 'una radice'); +is_deeply( + [$collections->get_leaf_collections()], + [$collections[0]], + 'una foglia'); } WebCoso::Config->clear(); @@ -176,7 +210,8 @@ EOF ok(WebCoso::Config->read_scalar($conf_file,"$thisdir/config-in-test"), 'eseguita la configurazione (no strict)'); -my @collections=WebCoso::Config->get_all_collections(); +my $collections=WebCoso::Config->get_collections(); +my @collections=$collections->get_all_collections(); is(scalar @collections,2,'due collezioni'); is_deeply( @@ -195,4 +230,12 @@ is_deeply( [$collections[1]->get_parents()], [$collections[0]], 'c1 padre di c2'); +is_deeply( + [$collections->get_root_collections()], + [], + 'nessuna radice'); +is_deeply( + [$collections->get_leaf_collections()], + [], + 'nessuna foglia'); } |