diff options
| author | franck cuny <franck@lumberjaph.net> | 2010-07-16 14:03:21 +0200 |
|---|---|---|
| committer | franck cuny <franck@lumberjaph.net> | 2010-07-16 14:03:21 +0200 |
| commit | a279b3fb49d786e7c711fa7156af9bbd0d2a9829 (patch) | |
| tree | b89f68fb8e4159f76cce64d3881a35c776ace58e /lib/Graph/GEXF/Role/Attributes.pm | |
| parent | basic gexf generation (diff) | |
| download | graph-gexf-a279b3fb49d786e7c711fa7156af9bbd0d2a9829.tar.gz | |
add attributes to edge
Diffstat (limited to 'lib/Graph/GEXF/Role/Attributes.pm')
| -rw-r--r-- | lib/Graph/GEXF/Role/Attributes.pm | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/Graph/GEXF/Role/Attributes.pm b/lib/Graph/GEXF/Role/Attributes.pm index 644793d..e03f5b1 100644 --- a/lib/Graph/GEXF/Role/Attributes.pm +++ b/lib/Graph/GEXF/Role/Attributes.pm @@ -7,12 +7,22 @@ parameter for => ( required => 1, ); +parameter with_method => ( + is => 'ro', + default => 0, +); + role { my $p = shift; foreach my $type (@{$p->for}) { - my $attr_name = $type . '_attributes'; + 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'], @@ -21,15 +31,30 @@ role { lazy => 1, default => sub { {} }, handles => { - 'attributes_' . $type . '_total' => 'count', - 'set_' . $type . '_attribute' => 'set', - 'get_' . $type . '_attribute' => 'get', - 'attributes_' . $type . '_list' => 'keys', - 'has_'.$type.'_attribute' => 'exists', + $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; |
