File Coverage

blib/lib/TreePath/Role/Graph.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package TreePath::Role::Graph;
2             $TreePath::Role::Graph::VERSION = '0.02';
3 1     1   22902 use Moose::Role;
  0            
  0            
4             use MooseX::Types::Path::Class;
5             use GraphViz2;
6              
7             requires 'tree';
8              
9             has 'output' => ( is => 'rw',
10             isa => 'Path::Class::File',
11             coerce => 1,
12             required => 1,
13             default => sub { '/tmp/tpgraph.png' },
14             );
15              
16             has 'colors' => (is => 'rw',
17             isa => 'HashRef',
18             );
19              
20             sub graph {
21             my $self = shift;
22             my $var = shift;
23              
24             my $g = GraphViz2->new();
25             foreach my $id ( sort { $a <=> $b } keys %{$self->tree}) {
26              
27             my $node = $self->tree->{$id};
28             my $parent = $node->{parent};
29              
30             my @keys_children = sort grep {/^children.*/} keys %$node;
31              
32             my $label_keys_children = '';
33             foreach my $k ( @keys_children ) {
34             $label_keys_children .= "<_${k}_>$k|";
35             }
36             if ( defined $label_keys_children && $label_keys_children ne '') {
37             chop($label_keys_children);
38             $label_keys_children = "{$label_keys_children}|";
39             }
40              
41             my $position = '';
42             $position = " / pos:" . $node->{$self->_position_field}
43             if defined $node->{$self->_position_field};
44              
45             my $label = "{ $label_keys_children "
46             . $node->{$self->_search_field}
47             . " (". $node->{$self->_type_field}
48             . "$position"
49             .")}";
50              
51             my $fg_color = 'black';
52             if ( defined $self->colors && defined $self->colors->{$node->{$self->_type_field}}) {
53             $fg_color = $self->colors->{$node->{$self->_type_field}}->{fg};
54             }
55             $g->add_node( name => $node->{id}, label => $label, shape => 'record', color => $fg_color);
56              
57             if ( $node->{parent} ){
58             my $key_children = $self->_key_children($node, $parent);
59             $g->add_edge( from => $node->{id}, to => "$parent->{id}:_${key_children}_" );
60              
61             }
62             }
63              
64             $g-> run(format => 'png', output_file => $self->output);
65             $self->_log($self->output . " generate.");
66             }
67              
68              
69              
70             =head1 NAME
71              
72             TreePath::Role::Graph - Role to visualize TreePath Graph
73              
74             =head1 VERSION
75              
76             version 0.02
77              
78             =head1 SYNOPSIS
79              
80             package TPGraph;
81             use Moose;
82              
83             extends 'TreePath';
84             with 'TreePath::Role::Graph';
85              
86             1;
87              
88             use TPGraph;
89              
90             my $colors_type = {
91             'T1' => { fg => 'blue'},
92             'T2' => { fg => 'magenta'},
93             'T3' => { fg => 'brown'},
94             };
95              
96             # get tree from hash, dbix, file
97             $tp = TPGraph->new( conf => $tree,
98             colors => $colors_type,
99             output => '/tmp/test.png' );
100              
101             $tp->graph; # all the tree
102              
103             $tp->graph($node);
104              
105             =head1 METHODS
106              
107             =head2 graph
108              
109             $tp->graph
110              
111             =cut
112              
113             =head1 SEE ALSO
114              
115             L<TreePath>
116              
117             =cut
118              
119             =head1 AUTHOR
120              
121             Daniel Brosseau, C<< <dab at catapullse.org> >>
122              
123             =head1 BUGS
124              
125             Please report any bugs or feature requests to C<bug-treepath-role-graph at rt.cpan.org>, or through
126             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=TreePath-Role-Graph>. I will be notified, and then you'll
127             automatically be notified of progress on your bug as I make changes.
128              
129              
130              
131              
132             =head1 SUPPORT
133              
134             You can find documentation for this module with the perldoc command.
135              
136             perldoc TreePath::Role::Graph
137              
138              
139             You can also look for information at:
140              
141             =over 4
142              
143             =item * RT: CPAN's request tracker (report bugs here)
144              
145             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=TreePath-Role-Graph>
146              
147             =item * AnnoCPAN: Annotated CPAN documentation
148              
149             L<http://annocpan.org/dist/TreePath-Role-Graph>
150              
151             =item * CPAN Ratings
152              
153             L<http://cpanratings.perl.org/d/TreePath-Role-Graph>
154              
155             =item * Search CPAN
156              
157             L<http://search.cpan.org/dist/TreePath-Role-Graph/>
158              
159             =back
160              
161              
162             =head1 ACKNOWLEDGEMENTS
163              
164              
165             =head1 LICENSE AND COPYRIGHT
166              
167             Copyright 2015 Daniel Brosseau.
168              
169             This program is free software; you can redistribute it and/or modify it
170             under the terms of the the Artistic License (2.0). You may obtain a
171             copy of the full license at:
172              
173             L<http://www.perlfoundation.org/artistic_license_2_0>
174              
175             Any use, modification, and distribution of the Standard or Modified
176             Versions is governed by this Artistic License. By using, modifying or
177             distributing the Package, you accept this license. Do not use, modify,
178             or distribute the Package, if you do not accept this license.
179              
180             If your Modified Version has been derived from a Modified Version made
181             by someone other than you, you are nevertheless required to ensure that
182             your Modified Version complies with the requirements of this license.
183              
184             This license does not grant you the right to use any trademark, service
185             mark, tradename, or logo of the Copyright Holder.
186              
187             This license includes the non-exclusive, worldwide, free-of-charge
188             patent license to make, have made, use, offer to sell, sell, import and
189             otherwise transfer the Package with respect to any patent claims
190             licensable by the Copyright Holder that are necessarily infringed by the
191             Package. If you institute patent litigation (including a cross-claim or
192             counterclaim) against any party alleging that the Package constitutes
193             direct or contributory patent infringement, then this Artistic License
194             to you shall terminate on the date that such litigation is filed.
195              
196             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
197             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
198             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
199             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
200             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
201             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
202             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
203             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
204              
205              
206             =cut
207              
208             1; # End of TreePath::Role::Graph