Formatter.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary Formatter/Utils.pm »
1 package Map::Tube::Plugin::Formatter;
2  
3 $Map::Tube::Plugin::Formatter::VERSION = '0.16';
4 $Map::Tube::Plugin::Formatter::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Map::Tube::Plugin::Formatter - Formatter plugin for Map::Tube.
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 YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Map::Tube::Plugin::Formatter::Utils qw(xml get_data);
Subroutine BEGIN: called CC=1 SCAR=2.6
20  
21 use Moo::Role;
22 use namespace::autoclean;
23  
24 =head1 DESCRIPTION
25  
26 A very simple add-on for L<Map::Tube> to format the supported objects.
27  
28 =head1 SYNOPSIS
29  
30 use strict; use warnings;
31 use Map::Tube::London;
32  
33 my $map = Map::Tube::London->new;
34  
35 my $node = $map->get_node_by_name('Baker Street');
36 print $node->to_xml, "\n\n";
37 print $node->to_json, "\n\n";
38 print $node->to_yaml, "\n\n";
39 print $node->to_string, "\n\n";
40  
41 my $line = $map->get_line_by_name('Metropolitan');
42 print $line->to_xml, "\n\n";
43 print $line->to_json, "\n\n";
44 print $line->to_yaml, "\n\n";
45 print $line->to_string, "\n\n";
46  
47 my $route = $map->get_shortest_route('Baker Street', 'Wembley Park');
48 print $route->to_xml, "\n\n";
49 print $route->to_json, "\n\n";
50 print $route->to_yaml, "\n\n";
51 print $route->to_string,"\n\n";
52  
53 =head1 SUPPORTED FORMATS
54  
55 It currently supports the following formats.
56  
57 =over 4
58  
59 =item * XML
60  
61 =item * JSON
62  
63 =item * YAML
64  
65 =item * STRING
66  
67 =back
68  
69 =head1 SUPPORTED OBJECTS
70  
71 It currently supports the following objects.
72  
73 =over 4
74  
75 =item * L<Map::Tube::Node>
76  
77 =item * L<Map::Tube::Line>
78  
79 =item * L<Map::Tube::Route>
80  
81 =back
82  
83 =head1 METHODS
84  
85 =head2 to_xml()
86  
87 Returns XML representation of the object.
88  
89 =cut
90  
91 sub to_xml {
92 my ($self) = @_;
93  
94 my $data = {};
95 if (ref($self) eq 'Map::Tube::Node') {
96 $data = {
97 node => {
98 attributes => {
99 id => $self->id,
100 name => $self->name,
101 },
102 children => {
103 link => [ map {{ id => $_->id, name => $_->name }} (@{$self->links}) ],
104 line => [ map {{ id => $_->id, name => $_->name }} (@{$self->line}) ],
105 },
106 },
107 };
108 }
109 elsif (ref($self) eq 'Map::Tube::Line') {
110 $data = {
111 line => {
112 attributes => {
113 id => $self->id,
114 name => $self->name,
115 color => $self->color || 'undef',
116 },
117 children => {
118 station => [ map {{ id => $_->id, name => $_->name }} (@{$self->get_stations}) ],
119 },
120 },
121 };
122 }
123 elsif (ref($self) eq 'Map::Tube::Route') {
124 my $children = {};
125 my $nodes = $self->nodes;
126 my $size = $#$nodes;
127 foreach my $i (1..($size-1)) {
128 push @{$children->{node}}, { name => $nodes->[$i]->as_string, order => $i };
129 }
130  
131 $data = {
132 route => {
133 attributes => {
134 from => $self->from->as_string,
135 to => $self->to->as_string,
136 },
137 children => $children,
138 },
139 };
140 }
141  
142 return xml($data);
143  
144 }
145  
146 =head2 to_json()
147  
148 Returns JSON representation of the object.
149  
150 =cut
151  
152 sub to_json {
153 my ($self) = @_;
154  
155 return JSON->new->utf8(1)->pretty->encode(get_data($self));
156 }
157  
158 =head2 to_yaml()
159  
160 Returns YAML representation of the object.
161  
162 =cut
163  
164 sub to_yaml {
165 my ($self) = @_;
166  
167 return Dump(get_data($self));
168 }
169  
170 =head2 to_string()
171  
172 Returns STRING representation of the object.
173  
174 =cut
175  
176 sub to_string {
177 my ($self) = @_;
178  
179 return $self->as_string;
180 }
181  
182 =head1 AUTHOR
183  
184 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
185  
186 =head1 REPOSITORY
187  
188 L<https://github.com/manwar/Map-Tube-Plugin-Formatter>
189  
190 =head1 SEE ALSO
191  
192 =over 4
193  
194 =item * L<Map::Tube>
195  
196 =back
197  
198 =head1 BUGS
199  
200 Please report any bugs or feature requests to C<bug-map-tube-plugin-formatter at rt.cpan.org>,
201 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Tube-Plugin-Formatter>.
202 I will be notified and then you'll automatically be notified of progress on your
203 bug as I make changes.
204  
205 =head1 SUPPORT
206  
207 You can find documentation for this module with the perldoc command.
208  
209 perldoc Map::Tube::Plugin::Formatter
210  
211 You can also look for information at:
212  
213 =over 4
214  
215 =item * RT: CPAN's request tracker (report bugs here)
216  
217 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Map-Tube-Plugin-Formatter>
218  
219 =item * AnnoCPAN: Annotated CPAN documentation
220  
221 L<http://annocpan.org/dist/Map-Tube-Plugin-Formatter>
222  
223 =item * CPAN Ratings
224  
225 L<http://cpanratings.perl.org/d/Map-Tube-Plugin-Formatter>
226  
227 =item * Search CPAN
228  
229 L<http://search.cpan.org/dist/Map-Tube-Plugin-Formatter/>
230  
231 =back
232  
233 =head1 LICENSE AND COPYRIGHT
234  
235 Copyright (C) 2015 - 2016 Mohammad S Anwar.
236  
237 This program is free software; you can redistribute it and / or modify it under
238 the terms of the the Artistic License (2.0). You may obtain a copy of the full
239 license at:
240  
241 L<http://www.perlfoundation.org/artistic_license_2_0>
242  
243 Any use, modification, and distribution of the Standard or Modified Versions is
244 governed by this Artistic License.By using, modifying or distributing the Package,
245 you accept this license. Do not use, modify, or distribute the Package, if you do
246 not accept this license.
247  
248 If your Modified Version has been derived from a Modified Version made by someone
249 other than you,you are nevertheless required to ensure that your Modified Version
250 complies with the requirements of this license.
251  
252 This license does not grant you the right to use any trademark, service mark,
253 tradename, or logo of the Copyright Holder.
254  
255 This license includes the non-exclusive, worldwide, free-of-charge patent license
256 to make, have made, use, offer to sell, sell, import and otherwise transfer the
257 Package with respect to any patent claims licensable by the Copyright Holder that
258 are necessarily infringed by the Package. If you institute patent litigation
259 (including a cross-claim or counterclaim) against any party alleging that the
260 Package constitutes direct or contributory patent infringement,then this Artistic
261 License to you shall terminate on the date that such litigation is filed.
262  
263 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
264 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
265 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
266 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
267 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
268 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
269 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270  
271 =cut
272  
273 1; # End of Map::Tube::Plugin::Formatter
↑ Summary Formatter/Utils.pm »