File Coverage

blib/lib/HTML/Widgets/NavMenu/ToJSON.pm
Criterion Covered Total %
statement 42 42 100.0
branch 6 8 75.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 59 61 96.7


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