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