From 175e61d6f35f8573b6efb844c524a90c2dba8c22 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 5 Feb 2006 10:49:52 +0000 Subject: spostate le collection fuori da config:: git-svn-id: svn://luxion/repos/WebCoso/trunk@152 fcb26f47-9200-0410-b104-b98ab5b095f3 --- lib/WebCoso/Collection.pm | 133 ++++++++++++++++++++++++++++++++++++++ lib/WebCoso/Collections.pm | 74 +++++++++++++++++++++ lib/WebCoso/Config.pm | 10 +-- lib/WebCoso/Config/Collection.pm | 133 -------------------------------------- lib/WebCoso/Config/Collections.pm | 74 --------------------- t/01-config.t | 2 +- 6 files changed, 213 insertions(+), 213 deletions(-) create mode 100644 lib/WebCoso/Collection.pm create mode 100644 lib/WebCoso/Collections.pm delete mode 100644 lib/WebCoso/Config/Collection.pm delete mode 100644 lib/WebCoso/Config/Collections.pm diff --git a/lib/WebCoso/Collection.pm b/lib/WebCoso/Collection.pm new file mode 100644 index 0000000..42442f1 --- /dev/null +++ b/lib/WebCoso/Collection.pm @@ -0,0 +1,133 @@ +package WebCoso::Collection; +use strict; +use warnings; +use Class::Std; +use Scalar::Util 'weaken'; +use List::MoreUtils 'any'; +use WebCoso::Config; + +{ +my %names_of :ATTR( :get ); +my %parents_of :ATTR( :get ); +my %children_of :ATTR( :get ); +my %resources_of :ATTR( :init_arg :get ); + +sub BUILD { + my ($self,$ident,$args_ref)=@_; + + my $names=$args_ref->{name}; + # trasformo un nome semplice in un nome "per qualsiasi lingua" + $names={''=>$names} unless ref($names) eq 'HASH'; + $names_of{$ident}=$names; + + my $parents=$args_ref->{parents} || []; + $parents_of{$ident}=$parents; + $_->add_child($self) for @$parents; + + my $children=$args_ref->{children} || []; + $children_of{$ident}=$children; + $_->add_parent($self) for @$children; + + $resources_of{$ident}=[]; + + WebCoso::Config->add_collection($self); + + return; +} + +sub get_axes { + return 'language'; +} + +sub get_axis_values { + my ($self,$axis_name)=@_; + if ($axis_name eq 'language') { + return grep { $_ } keys %{ $self->get_names() } + } + else { + return; + } +} + +sub get_properties { + my ($self,$axis_name,$axis_value,@rest)=@_; + + if (@rest==0 and $axis_name eq 'language') { + 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; + } + } +} + +sub add_child { + my ($self,$child)=@_; + + return if any { $_ eq $child } @{ $self->get_children_ref() }; + + push @{ $self->get_children_ref() },$child; + $child->add_parent($self); + + return; +} +sub add_parent { + my ($self,$parent)=@_; + + return if any { $_ eq $parent } @{ $self->get_parents_ref() }; + + my $weak_parent=$parent; + weaken $weak_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 { + my ($self)=@_; + return @{ $self->get_parents_ref() }; +} +sub get_children { + my ($self)=@_; + return @{ $self->get_children_ref() }; +} +sub get_resources { + my ($self)=@_; + return @{ $self->get_resources_ref() }; +} + +} + +1; diff --git a/lib/WebCoso/Collections.pm b/lib/WebCoso/Collections.pm new file mode 100644 index 0000000..f0f9815 --- /dev/null +++ b/lib/WebCoso/Collections.pm @@ -0,0 +1,74 @@ +package WebCoso::Collections; +use strict; +use warnings; +use Class::Std; +use List::MoreUtils 'any'; + +{ +my %collections_of :ATTR( :get ); + +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 get_axes { + return 'language'; +} + +sub get_axis_values { + 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->get_axis_values('language') } = (); + } + + delete $langs{''}; + + return keys %langs; +} + +} + +1; diff --git a/lib/WebCoso/Config.pm b/lib/WebCoso/Config.pm index 78c9dbe..ab62179 100644 --- a/lib/WebCoso/Config.pm +++ b/lib/WebCoso/Config.pm @@ -1,13 +1,13 @@ package WebCoso::Config; use strict; use warnings; -use WebCoso::Config::Collections; +use WebCoso::Collections; use WebCoso::Pipeline; use WebCoso::X; use utf8; my @resources; -my $collections=WebCoso::Config::Collections->new(); +my $collections=WebCoso::Collections->new(); my %resource_to_pipeline; sub read_scalar { @@ -60,14 +60,14 @@ sub get_pipeline_for { sub clear { @resources=(); - $collections=WebCoso::Config::Collections->new(); + $collections=WebCoso::Collections->new(); %resource_to_pipeline=(); } package WebCoso::Config::Helpers; use Path::Class; use WebCoso::Resource; -use WebCoso::Config::Collection; +use WebCoso::Collection; use Exporter::Lite; use vars qw($FILENAME @EXPORT); @@ -123,7 +123,7 @@ sub res { sub coll { my ($name,$parents,$children,$resources)=@_; - my $collection=WebCoso::Config::Collection->new({ + my $collection=WebCoso::Collection->new({ name=>$name, parents=>$parents||[], children=>$children||[], diff --git a/lib/WebCoso/Config/Collection.pm b/lib/WebCoso/Config/Collection.pm deleted file mode 100644 index 23b1aea..0000000 --- a/lib/WebCoso/Config/Collection.pm +++ /dev/null @@ -1,133 +0,0 @@ -package WebCoso::Config::Collection; -use strict; -use warnings; -use Class::Std; -use Scalar::Util 'weaken'; -use List::MoreUtils 'any'; -use WebCoso::Config; - -{ -my %names_of :ATTR( :get ); -my %parents_of :ATTR( :get ); -my %children_of :ATTR( :get ); -my %resources_of :ATTR( :init_arg :get ); - -sub BUILD { - my ($self,$ident,$args_ref)=@_; - - my $names=$args_ref->{name}; - # trasformo un nome semplice in un nome "per qualsiasi lingua" - $names={''=>$names} unless ref($names) eq 'HASH'; - $names_of{$ident}=$names; - - my $parents=$args_ref->{parents} || []; - $parents_of{$ident}=$parents; - $_->add_child($self) for @$parents; - - my $children=$args_ref->{children} || []; - $children_of{$ident}=$children; - $_->add_parent($self) for @$children; - - $resources_of{$ident}=[]; - - WebCoso::Config->add_collection($self); - - return; -} - -sub get_axes { - return 'language'; -} - -sub get_axis_values { - my ($self,$axis_name)=@_; - if ($axis_name eq 'language') { - return grep { $_ } keys %{ $self->get_names() } - } - else { - return; - } -} - -sub get_properties { - my ($self,$axis_name,$axis_value,@rest)=@_; - - if (@rest==0 and $axis_name eq 'language') { - 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; - } - } -} - -sub add_child { - my ($self,$child)=@_; - - return if any { $_ eq $child } @{ $self->get_children_ref() }; - - push @{ $self->get_children_ref() },$child; - $child->add_parent($self); - - return; -} -sub add_parent { - my ($self,$parent)=@_; - - return if any { $_ eq $parent } @{ $self->get_parents_ref() }; - - my $weak_parent=$parent; - weaken $weak_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 { - my ($self)=@_; - return @{ $self->get_parents_ref() }; -} -sub get_children { - my ($self)=@_; - return @{ $self->get_children_ref() }; -} -sub get_resources { - my ($self)=@_; - return @{ $self->get_resources_ref() }; -} - -} - -1; diff --git a/lib/WebCoso/Config/Collections.pm b/lib/WebCoso/Config/Collections.pm deleted file mode 100644 index 7c70e64..0000000 --- a/lib/WebCoso/Config/Collections.pm +++ /dev/null @@ -1,74 +0,0 @@ -package WebCoso::Config::Collections; -use strict; -use warnings; -use Class::Std; -use List::MoreUtils 'any'; - -{ -my %collections_of :ATTR( :get ); - -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 get_axes { - return 'language'; -} - -sub get_axis_values { - 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->get_axis_values('language') } = (); - } - - delete $langs{''}; - - return keys %langs; -} - -} - -1; diff --git a/t/01-config.t b/t/01-config.t index f5dd870..79bca63 100644 --- a/t/01-config.t +++ b/t/01-config.t @@ -80,7 +80,7 @@ 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_collections(); -isa_ok($collections,'WebCoso::Config::Collections'); +isa_ok($collections,'WebCoso::Collections'); my @collections=$collections->get_all_collections(); is(scalar @collections,1,'una collezione'); -- cgit v1.2.3