summaryrefslogtreecommitdiff
path: root/lib/Graph/GEXF/Role/Attributes.pm
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2011-06-13 18:38:56 +0200
committerfranck cuny <franck@lumberjaph.net>2011-06-13 18:38:56 +0200
commita972089fecb01c62880c6a2a5bc5cbcb96105580 (patch)
tree87468ea945f6e5fb457249142e1d4b5f5dc71c5a /lib/Graph/GEXF/Role/Attributes.pm
parentadd flash (diff)
downloadstargit-a972089fecb01c62880c6a2a5bc5cbcb96105580.tar.gz
add Graph::GEXF while it's not on CPAN
Signed-off-by: franck cuny <franck@lumberjaph.net>
Diffstat (limited to '')
-rw-r--r--lib/Graph/GEXF/Role/Attributes.pm60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/Graph/GEXF/Role/Attributes.pm b/lib/Graph/GEXF/Role/Attributes.pm
new file mode 100644
index 0000000..e03f5b1
--- /dev/null
+++ b/lib/Graph/GEXF/Role/Attributes.pm
@@ -0,0 +1,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;