File Coverage

blib/lib/Yukki/Web/Response.pm
Criterion Covered Total %
statement 25 43 58.1
branch 0 2 0.0
condition 0 8 0.0
subroutine 9 15 60.0
pod 6 6 100.0
total 40 74 54.0


line stmt bran cond sub pod time code
1             package Yukki::Web::Response;
2             $Yukki::Web::Response::VERSION = '0.991_002'; # TRIAL
3              
4 1     1   15 $Yukki::Web::Response::VERSION = '0.991002';use v5.24;
  1         3  
5 1     1   4 use utf8;
  1         3  
  1         6  
6 1     1   19 use Moo;
  1         1  
  1         7  
7              
8 1     1   304 use Type::Utils;
  1         1  
  1         7  
9 1     1   1248 use Types::Standard qw( Str );
  1         1  
  1         7  
10 1     1   777 use Yukki::Types qw( BreadcrumbLinks NavigationMenuMap );
  1         3  
  1         10  
11              
12 1     1   903 use Plack::Response;
  1         1238  
  1         26  
13              
14 1     1   6 use namespace::clean;
  1         2  
  1         13  
15              
16             # ABSTRACT: the response to the client
17              
18              
19             has response => (
20             is => 'ro',
21             isa => class_type('Plack::Response'),
22             required => 1,
23             lazy => 1,
24             builder => '_build_response',
25             handles => [ qw(
26             status headers body header content_type content_length content_encoding
27             redirect location cookies finalize
28             ) ],
29             );
30              
31             sub _build_response {
32 1     1   188 my $self = shift;
33 1         15 return Plack::Response->new(200, [ 'Content-type' => 'text/html; charset=utf-8' ]);
34             }
35              
36              
37             has page_title => (
38             is => 'rw',
39             isa => Str,
40             predicate => 'has_page_title',
41             );
42              
43              
44             has navigation => (
45             is => 'rw',
46             isa => NavigationMenuMap,
47             required => 1,
48             default => sub { +{} },
49             );
50              
51             sub navigation_menu_names {
52 0     0 1   my $self = shift;
53 0           keys $self->navigation->%*;
54             }
55              
56              
57             has breadcrumb => (
58             is => 'rw',
59             isa => BreadcrumbLinks,
60             required => 1,
61             default => sub { [] },
62             );
63              
64             sub breadcrumb_links {
65 0     0 1   my $self = shift;
66 0           $self->breadcrumb->@*;
67             }
68              
69             sub has_breadcrumb {
70 0     0 1   my $self = shift;
71 0           scalar $self->breadcrumb->@*;
72             }
73              
74              
75             sub navigation_menu {
76 0     0 1   my ($self, $name) = @_;
77 0   0       return sort { ($a->{sort}//50) <=> ($b->{sort}//50) }
      0        
78 0   0       @{ $self->navigation->{$name} // [] };
  0            
79             }
80              
81              
82 0     0 1   sub add_navigation_item { shift->add_navigation_items(@_) }
83              
84             sub add_navigation_items {
85 0     0 1   my $self = shift;
86 0           my $name_or_names = shift;
87              
88 0 0         my @names = ref $name_or_names ? @$name_or_names : ($name_or_names);
89              
90 0           for my $name (@names) {
91 0   0       $self->navigation->{$name} //= [];
92 0           push @{ $self->navigation->{$name} }, @_;
  0            
93             }
94             }
95              
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Yukki::Web::Response - the response to the client
108              
109             =head1 VERSION
110              
111             version 0.991_002
112              
113             =head1 DESCRIPTION
114              
115             An abstraction around the HTTP response that is astonishingly similar to L<Plack::Response>. Call C<finalize> to get the final PSGI response.
116              
117             =head1 ATTRIBUTES
118              
119             =head2 response
120              
121             This is the internal L<Plack::Response> object. Do not use.
122              
123             Use the delegated methods instead:
124              
125             status headers body header content_type content_length content_encoding
126             redirect location cookies finalize
127              
128             =head2 page_title
129              
130             This is the title to give the page in the HTML.
131              
132             =head2 navigation
133              
134             This is the navigation menu to place in the page. This is an array of hashes. Each entry should look like:
135              
136             {
137             label => 'Label',
138             href => '/link/to/somewhere',
139             sort => 50,
140             }
141              
142             A sorted list of items is retrieved using L</navigation_menu>. New items can be added with the L</add_navigation_item> and L</add_navigation_items> methods.
143              
144             =head2 breadcrumb
145              
146             This is the breadcrumb to display. It is an empty array by default (meaning no breadcrumb). Each element of the breadcrumb is formatted like navigation, except that C<sort> is not used here.
147              
148             =head1 METHODS
149              
150             =head2 navigation_menu
151              
152             my @items = $response->navigation_menu('repository');
153              
154             Returns a sorted list of navigation items for the named menu.
155              
156             =head2 add_navigation_item
157              
158             =head2 add_navigation_items
159              
160             $response->add_navigation_item(menu_name => {
161             label => 'Link Title',
162             url => '/path/to/some/place',
163             sort => 50,
164             });
165              
166             Add one or more items to the named menu. The first argument is always the name or names of the menu. Mutliple names may be given in an array reference. If multiple names are given, the menu items given will be added to each menu named. The remaining arguments are hash references that must have a C<label> and a C<url>. The C<sort> is optional.
167              
168             L</add_navigation_item> is a synonym for L</add_navigation_items>.
169              
170             =head2 breadcrumb_links
171              
172             Convenience accessor that returns C<breadcrumbs> as a list.
173              
174             =head2 has_breadcrumb
175              
176             Returns a true value if C<breadcrumbs> has any items in it.
177              
178             =head2 has_page_title
179              
180             Returns a true value if C<page_title> is set.
181              
182             =head2 navigation_menu_names
183              
184             Convenience accessor that returns C<navigation> as a list.
185              
186             =head1 AUTHOR
187              
188             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
189              
190             =head1 COPYRIGHT AND LICENSE
191              
192             This software is copyright (c) 2017 by Qubling Software LLC.
193              
194             This is free software; you can redistribute it and/or modify it under
195             the same terms as the Perl 5 programming language system itself.
196              
197             =cut