| 1 | package HTML::Widgets::NavMenu::ToJSON; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use parent 'HTML::Widgets::NavMenu::Object'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use JSON::MaybeXS (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | HTML::Widgets::NavMenu::ToJSON - convert HTML::Widgets::NavMenu to JSON | ||||||||||
| 16 | |||||||||||
| 17 | =head1 VERSION | ||||||||||
| 18 | |||||||||||
| 19 | Version 0.0.3 | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | 3 | our $VERSION = '0.0.3'; | |||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | use HTML::Widgets::NavMenu::ToJSON; | ||||||||||
| 28 | use HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML; | ||||||||||
| 29 | |||||||||||
| 30 | my $persistence = | ||||||||||
| 31 | HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML->new( | ||||||||||
| 32 | { | ||||||||||
| 33 | filename => '/path/to/persistence_data.yaml', | ||||||||||
| 34 | } | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | my $obj = HTML::Widgets::NavMenu::ToJSON->new( | ||||||||||
| 38 | { | ||||||||||
| 39 | data_persistence_store => $persistence, | ||||||||||
| 40 | # The one given as input to HTML::Widgets::NavMenu | ||||||||||
| 41 | tree_contents => $tree_contents, | ||||||||||
| 42 | } | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | use IO::All; | ||||||||||
| 46 | |||||||||||
| 47 | io->file('output.json')->println( | ||||||||||
| 48 | $obj->output_as_json( | ||||||||||
| 49 | { | ||||||||||
| 50 | %args | ||||||||||
| 51 | } | ||||||||||
| 52 | ) | ||||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SUBROUTINES/METHODS | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 3 | __PACKAGE__->mk_acc_ref( | |||||||||
| 60 | [ | ||||||||||
| 61 | qw( | ||||||||||
| 62 | _data_persistence_store | ||||||||||
| 63 | _tree_contents | ||||||||||
| 64 | ), | ||||||||||
| 65 | ] | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | sub _init | ||||||||||
| 69 | { | ||||||||||
| 70 | 1 | ▶ | my ($self, $args) = @_; | ||||||||
|
Subroutine
_init: called CC=3 SCAR=11.4
| |||||||||||
| 71 | |||||||||||
| 72 | $self->_data_persistence_store( | ||||||||||
| 73 | 1 | ▶ | $args->{'data_persistence_store'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | ) or Carp::confess("No data_persistence_store specified."); | ||||||||||
| 75 | |||||||||||
| 76 | $self->_tree_contents( | ||||||||||
| 77 | 1 | ▶ | $args->{'tree_contents'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | ) or Carp::confess("No tree_contents specified."); | ||||||||||
| 79 | |||||||||||
| 80 | 1 | return; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | sub _get_id_for_url | ||||||||||
| 84 | { | ||||||||||
| 85 | 9 | ▶ | my ($self, $url) = @_; | ||||||||
|
Subroutine
_get_id_for_url: called CC=1 SCAR=0.0
| |||||||||||
| 86 | 9 | return $self->_data_persistence_store->get_id_for_url($url); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | =head2 $self->output_as_json() | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | sub output_as_json | ||||||||||
| 94 | { | ||||||||||
| 95 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
output_as_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | |||||||||||
| 97 | 1 | my $persistence = $self->_data_persistence_store(); | |||||||||
| 98 | |||||||||||
| 99 | 1 | $persistence->load; | |||||||||
| 100 | |||||||||||
| 101 | 1 | my $process_sub_tree; | |||||||||
| 102 | |||||||||||
| 103 | $process_sub_tree = sub | ||||||||||
| 104 | { | ||||||||||
| 105 | 10 | ▶ | my ($sub_tree) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 106 | |||||||||||
| 107 | 10 | my @keys = (grep { $_ ne 'subs' } keys %{$sub_tree}); | |||||||||
| 108 | |||||||||||
| 109 | 10 | my $has_subs = exists($sub_tree->{subs}); | |||||||||
| 110 | |||||||||||
| 111 | return | ||||||||||
| 112 | { | ||||||||||
| 113 | (exists($sub_tree->{url}) | ||||||||||
| 114 | ? (id => $self->_get_id_for_url($sub_tree->{url}), ) | ||||||||||
| 115 | : () | ||||||||||
| 116 | ), | ||||||||||
| 117 | 28 | ▶ | (map { $_ => $sub_tree->{$_} } @keys), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | $has_subs | ||||||||||
| 119 | 9 | ? (subs => [ map { $process_sub_tree->($_) } | |||||||||
| 120 | 9 | grep { ! exists($_->{separator}) } | |||||||||
| 121 | 10 | ▶ | @{$sub_tree->{subs}} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | ]) | ||||||||||
| 123 | : (), | ||||||||||
| 124 | }; | ||||||||||
| 125 | 1 | }; | |||||||||
| 126 | |||||||||||
| 127 | my $ret = JSON::MaybeXS->new(utf8 => 1, canonical => 1)->encode( | ||||||||||
| 128 | 1 | $process_sub_tree->($self->_tree_contents)->{'subs'} | |||||||||
| 129 | ); | ||||||||||
| 130 | |||||||||||
| 131 | 1 | $persistence->save; | |||||||||
| 132 | |||||||||||
| 133 | 1 | return $ret; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | =head1 AUTHOR | ||||||||||
| 137 | |||||||||||
| 138 | Shlomi Fish, C<< <shlomif at cpan.org> >> | ||||||||||
| 139 | |||||||||||
| 140 | =head1 BUGS | ||||||||||
| 141 | |||||||||||
| 142 | Please report any bugs or feature requests to C<bug-html-widgets-navmenu-tojson at rt.cpan.org>, or through | ||||||||||
| 143 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Widgets-NavMenu-ToJSON>. I will be notified, and then you'll | ||||||||||
| 144 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 145 | |||||||||||
| 146 | =head1 SUPPORT | ||||||||||
| 147 | |||||||||||
| 148 | You can find documentation for this module with the perldoc command. | ||||||||||
| 149 | |||||||||||
| 150 | perldoc HTML::Widgets::NavMenu::ToJSON | ||||||||||
| 151 | |||||||||||
| 152 | |||||||||||
| 153 | You can also look for information at: | ||||||||||
| 154 | |||||||||||
| 155 | =over 4 | ||||||||||
| 156 | |||||||||||
| 157 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 158 | |||||||||||
| 159 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Widgets-NavMenu-ToJSON> | ||||||||||
| 160 | |||||||||||
| 161 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 162 | |||||||||||
| 163 | L<http://annocpan.org/dist/HTML-Widgets-NavMenu-ToJSON> | ||||||||||
| 164 | |||||||||||
| 165 | =item * CPAN Ratings | ||||||||||
| 166 | |||||||||||
| 167 | L<http://cpanratings.perl.org/d/HTML-Widgets-NavMenu-ToJSON> | ||||||||||
| 168 | |||||||||||
| 169 | =item * Search CPAN | ||||||||||
| 170 | |||||||||||
| 171 | L<http://search.cpan.org/dist/HTML-Widgets-NavMenu-ToJSON/> | ||||||||||
| 172 | |||||||||||
| 173 | =back | ||||||||||
| 174 | |||||||||||
| 175 | |||||||||||
| 176 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 177 | |||||||||||
| 178 | |||||||||||
| 179 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 180 | |||||||||||
| 181 | Copyright 2012 Shlomi Fish. | ||||||||||
| 182 | |||||||||||
| 183 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 184 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 185 | copy of the full license at: | ||||||||||
| 186 | |||||||||||
| 187 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 188 | |||||||||||
| 189 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 190 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 191 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 192 | or distribute the Package, if you do not accept this license. | ||||||||||
| 193 | |||||||||||
| 194 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 195 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 196 | your Modified Version complies with the requirements of this license. | ||||||||||
| 197 | |||||||||||
| 198 | This license does not grant you the right to use any trademark, service | ||||||||||
| 199 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 200 | |||||||||||
| 201 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 202 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 203 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 204 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 205 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 206 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 207 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 208 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 209 | |||||||||||
| 210 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 211 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 212 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 213 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 214 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 215 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 216 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 217 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 218 | |||||||||||
| 219 | |||||||||||
| 220 | =cut | ||||||||||
| 221 | |||||||||||
| 222 | 3 | 1; # End of HTML::Widgets::NavMenu::ToJSON | |||||||||