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
|
package Graph::GEXF::Role::Attributes;
use MooseX::Role::Parameterized;
parameter for => (
is => 'ro',
required => 1,
);
parameter with_method => (
is => 'ro',
default => 0,
);
role {
my $p = shift;
foreach my $type (@{$p->for}) {
my $attr_name = $type . '_attributes';
my $total_attr = 'attributes_' . $type . '_total';
my $set_attr = 'set_' . $type . '_attribute';
my $get_attr = 'get_' . $type . '_attribute';
my $list_attr = 'attributes_' . $type . '_list';
my $has_attr = 'has_' . $type . '_attribute';
has $attr_name => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef',
lazy => 1,
default => sub { {} },
handles => {
$total_attr => 'count',
$set_attr => 'set',
$get_attr => 'get',
$list_attr => 'keys',
$has_attr => 'exists',
}
);
if ($p->with_method) {
my $method_name = 'add_' . $type . '_attribute';
method $method_name => sub {
my ($self, $name, $type, $default_value) = @_;
my $id = $self->$total_attr();
my $attr = {
id => $id,
title => $name,
type => $type,
default => [$default_value],
};
$self->$set_attr($name => $attr);
};
}
}
};
1;
|