File Coverage

blib/lib/Map/Tube/Plugin/Formatter/Utils.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 23 91.3


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