aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso/Collection.pm
blob: 42442f124d8580a84753b97c63ed2d972bcaa1be (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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<names> );
my %parents_of :ATTR( :get<parents_ref> );
my %children_of :ATTR( :get<children_ref> );
my %resources_of :ATTR( :init_arg<resources> :get<resources_ref> );
 
sub BUILD {
    my ($self,$ident,$args_ref)=@_;
 
    my $names=$args_ref->{name};
    # trasformo un nome semplice in un nome "per qualsiasi lingua" 
    $names={''=>$namesunless ref($nameseq 'HASH';
    $names_of{$ident}=$names;
 
    my $parents=$args_ref->{parents} || [];
    $parents_of{$ident}=$parents;
    $_->add_child($selffor @$parents;
 
    my $children=$args_ref->{children} || [];
    $children_of{$ident}=$children;
    $_->add_parent($selffor @$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;