File Coverage

blib/lib/Forest/Tree/Roles/MetaData.pm
Criterion Covered Total %
statement 18 19 94.7
branch 5 8 62.5
condition n/a
subroutine 4 4 100.0
pod 1 2 50.0
total 28 33 84.8


line stmt bran cond sub pod time code
1             package Forest::Tree::Roles::MetaData;
2 3     3   10281 use Moose::Role;
  3         7  
  3         36  
3              
4             our $VERSION = '0.10';
5             our $AUTHORITY = 'cpan:STEVAN';
6              
7             has 'metadata' => (
8             is => 'rw',
9             isa => 'HashRef',
10             default => sub { {} },
11             );
12              
13             sub get_metadata_for {
14 1     1 0 3 my ($self, $key) = @_;
15              
16 1         38 return $self->metadata->{$key};
17             }
18              
19             sub fetch_metadata_for {
20 12     12 1 26 my ($self, $key) = @_;
21              
22 12         20 my $current = $self;
23              
24 12         17 do {
25 14 50       52 if ($current->does(__PACKAGE__)) {
26 14         1233 my $meta = $current->metadata;
27 14 100       105 return $meta->{$key}
28             if exists $meta->{$key};
29             }
30 3         115 $current = $current->parent;
31             } until $current->is_root;
32              
33 1 50       5 if ($current->does(__PACKAGE__)) {
34 1         77 my $meta = $current->metadata;
35 1 50       10 return $meta->{$key}
36             if exists $meta->{$key};
37             }
38              
39 0           return;
40             }
41              
42 3     3   20481 no Moose::Role; 1;
  3         9  
  3         22  
43              
44             __END__
45              
46             =pod
47              
48             =head1 NAME
49              
50             Forest::Tree::Roles::MetaData - A role mixin to support tree node metadata
51              
52             =head1 DESCRIPTION
53              
54             This role mixin adds support for each tree node to have arbitrary metadata
55             stored in a HASHref. The metadata is inherited in the tree as well, so a child
56             will inherit the parents metadata.
57              
58             This is really useful, at least for me it is :)
59              
60             =head1 ATTRIBUTES
61              
62             =over 4
63              
64             =item I<metadata>
65              
66             =back
67              
68             =head1 METHODS
69              
70             =over 4
71              
72             =item B<fetch_metadata_for ($key)>
73              
74             This will first check locally, if it doesn't fund anything then will climb
75             back to the root looking.
76              
77             =back
78              
79             =head1 BUGS
80              
81             All complex software has bugs lurking in it, and this module is no
82             exception. If you find a bug please either email me, or add the bug
83             to cpan-RT.
84              
85             =head1 AUTHOR
86              
87             Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
88              
89             =head1 COPYRIGHT AND LICENSE
90              
91             Copyright 2008-2014 Infinity Interactive, Inc.
92              
93             L<http://www.iinteractive.com>
94              
95             This library is free software; you can redistribute it and/or modify
96             it under the same terms as Perl itself.
97              
98             =cut