Formatter/Utils.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Formatter.pm ↑ Summary
1 package Map::Tube::Plugin::Formatter::Utils;
2  
3 $Map::Tube::Plugin::Formatter::Utils::VERSION = '0.16';
4 $Map::Tube::Plugin::Formatter::Utils::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Map::Tube::Plugin::Formatter::Utils - Helper package for Map::Tube::Plugin::Formatter.
9  
10 =head1 VERSION
11  
12 Version 0.16
13  
14 =cut
15  
16 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use parent 'Exporter';
21  
22 our @EXPORT_OK = qw(xml get_data);
23  
24 =head1 DESCRIPTION
25  
26 B<FOR INTERNAL USE ONLY>
27  
28 =cut
29  
30 sub xml {
31 my ($data) = @_;
32  
33 my $twig = XML::Twig
34 ->new()
35 ->set_xml_version("1.0")
36 ->set_encoding('utf-8');
37  
38 my $map = XML::Twig::Elt->new('map');
39 $twig->set_root($map);
40  
41 foreach my $i (keys %$data) {
42 my $e = $map->insert_new_elt($i, $data->{$i}->{attributes});
43 foreach my $j (keys %{$data->{$i}->{children}}) {
44 my $m = $e->insert_new_elt($j.'s');
45 foreach (@{$data->{$i}->{children}->{$j}}) {
46 $m->insert_new_elt($j, $_);
47 }
48 }
49 }
50  
51 $twig->set_pretty_print('indented');
52 return $twig->sprint;
53 }
54  
55 sub get_data {
56 my ($object) = @_;
57  
58 my $data = {};
59 if (ref($object) eq 'Map::Tube::Node') {
60 $data = {
61 id => $object->id,
62 name => $object->name,
63 links => [ map {{ id => $_->id, name => $_->name }} (@{$object->links}) ],
64 lines => [ map {{ id => $_->id, name => $_->name }} (@{$object->line}) ],
65 };
66 }
67 elsif (ref($object) eq 'Map::Tube::Line') {
68 $data = {
69 id => $object->id,
70 name => $object->name,
71 color => $object->color || 'undef',
72 stations => [ map {{ id => $_->id, name => $_->name }} (@{$object->get_stations}) ],
73 };
74 }
75 elsif (ref($object) eq 'Map::Tube::Route') {
76 my $children = [];
77 my $nodes = $object->nodes;
78 my $size = $#$nodes;
79 foreach my $i (1..($size-1)) {
80 push @{$children}, { name => $nodes->[$i]->as_string, order => $i };
81 }
82  
83 $data = {
84 from => $object->from->as_string,
85 to => $object->to->as_string,
86 nodes => $children,
87 };
88 }
89  
90 return $data;
91 }
92  
93 =head1 AUTHOR
94  
95 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
96  
97 =head1 REPOSITORY
98  
99 L<https://github.com/manwar/Map-Tube-Plugin-Formatter>
100  
101 =head1 BUGS
102  
103 Please report any bugs or feature requests to C<bug-map-tube-plugin-formatter at rt.cpan.org>,
104 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Tube-Plugin-Formatter>.
105 I will be notified and then you'll automatically be notified of progress on your
106 bug as I make changes.
107  
108 =head1 SUPPORT
109  
110 You can find documentation for this module with the perldoc command.
111  
112 perldoc Map::Tube::Plugin::Formatter::Utils
113  
114 You can also look for information at:
115  
116 =over 4
117  
118 =item * RT: CPAN's request tracker (report bugs here)
119  
120 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Map-Tube-Plugin-Formatter>
121  
122 =item * AnnoCPAN: Annotated CPAN documentation
123  
124 L<http://annocpan.org/dist/Map-Tube-Plugin-Formatter>
125  
126 =item * CPAN Ratings
127  
128 L<http://cpanratings.perl.org/d/Map-Tube-Plugin-Formatter>
129  
130 =item * Search CPAN
131  
132 L<http://search.cpan.org/dist/Map-Tube-Plugin-Formatter/>
133  
134 =back
135  
136 =head1 LICENSE AND COPYRIGHT
137  
138 Copyright (C) 2015 - 2016 Mohammad S Anwar.
139  
140 This program is free software; you can redistribute it and / or modify it under
141 the terms of the the Artistic License (2.0). You may obtain a copy of the full
142 license at:
143  
144 L<http://www.perlfoundation.org/artistic_license_2_0>
145  
146 Any use, modification, and distribution of the Standard or Modified Versions is
147 governed by this Artistic License.By using, modifying or distributing the Package,
148 you accept this license. Do not use, modify, or distribute the Package, if you do
149 not accept this license.
150  
151 If your Modified Version has been derived from a Modified Version made by someone
152 other than you,you are nevertheless required to ensure that your Modified Version
153 complies with the requirements of this license.
154  
155 This license does not grant you the right to use any trademark, service mark,
156 tradename, or logo of the Copyright Holder.
157  
158 This license includes the non-exclusive, worldwide, free-of-charge patent license
159 to make, have made, use, offer to sell, sell, import and otherwise transfer the
160 Package with respect to any patent claims licensable by the Copyright Holder that
161 are necessarily infringed by the Package. If you institute patent litigation
162 (including a cross-claim or counterclaim) against any party alleging that the
163 Package constitutes direct or contributory patent infringement,then this Artistic
164 License to you shall terminate on the date that such litigation is filed.
165  
166 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
167 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
168 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
169 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
170 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
171 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
172 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173  
174 =cut
175  
176 1; # End of Map::Tube::Plugin::Formatter::Utils
« Formatter.pm ↑ Summary