File Coverage

blib/lib/Dancer2/Plugin/PageHistory/Page.pm
Criterion Covered Total %
statement 26 26 100.0
branch 5 6 83.3
condition n/a
subroutine 7 7 100.0
pod 2 3 66.6
total 40 42 95.2


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::PageHistory::Page;
2              
3             =head1 NAME
4              
5             Dancer2::Plugin::PageHistory::Page - Page object for Dancer2::Plugin::PageHistory
6              
7             =cut
8              
9 6     6   95328 use Carp qw(croak);
  6         8  
  6         260  
10 6     6   772 use Dancer2::Core::Types qw(Str HashRef InstanceOf);
  6         15309  
  6         319  
11 6     6   883 use Moo;
  6         13005  
  6         23  
12 6     6   3563 use namespace::clean;
  6         15967  
  6         27  
13              
14             =head1 ATTRIBUTES
15              
16             =head2 attributes
17              
18             Extra attributes as a hash reference, e.g.: SKU for a product page.
19              
20             =cut
21              
22             has attributes => (
23             is => 'ro',
24             isa => HashRef,
25             predicate => 1,
26             );
27              
28             =head2 path
29              
30             Absolute path of the page. This is the path as seen by the Dancer2 application
31             so if the app is not mounted on '/' then this will be different from
32             L.
33              
34             If not passed in the constructor will be set from L.
35              
36             =cut
37              
38             has path => (
39             is => 'ro',
40             isa => Str,
41             lazy => 1,
42             default => sub {
43             return $_[0]->request->path
44             },
45             );
46              
47             =head2 request
48              
49             The L object. Optional.
50              
51             =cut
52              
53             has request => (
54             is => 'ro',
55             isa => InstanceOf ['Dancer2::Core::Request'],
56             clearer => '_clear_request',
57             );
58              
59             =head2 request_path
60              
61             The original request path but without the query string.
62              
63             If this was not supplied to the constructor then what this returns varies:
64              
65             If L was supplied then this is used to determine the appropriate
66             path to return.
67              
68             Otherwise L is returned.
69              
70             =cut
71              
72             has request_path => (
73             is => 'ro',
74             isa => Str,
75             lazy => 1,
76             default => sub {
77             my $self = shift;
78             if ( $self->request ) {
79             my $base = $self->request->base->path;
80             my $path = $self->path;
81             $base =~ s|/$||;
82             $path =~ s|^/||;
83             return "$base/$path";
84             }
85             else {
86             return $self->path;
87             }
88             },
89             );
90              
91             =head2 query_string
92              
93             The original query string.
94              
95             =cut
96              
97             has query_string => (
98             is => 'ro',
99             isa => Str,
100             lazy => 1,
101             default => sub {
102             my $self = shift;
103             return $self->request ? $self->request->env->{QUERY_STRING} : '';
104             },
105             );
106              
107             =head2 title
108              
109             Page title.
110              
111             =cut
112              
113             has title => (
114             is => 'ro',
115             isa => Str,
116             predicate => 1,
117             );
118              
119             =head1 METHODS
120              
121             =head2 BUILDARGS
122              
123             Checks that at least one of 'request' and 'path' have been supplied or croaks.
124              
125             =cut
126              
127             around BUILDARGS => sub {
128             my $orig = shift;
129             my $class = shift;
130             my %args = @_ == 1 && ref( $_[0] ) eq 'HASH' ? %{ $_[0] } : @_;
131              
132             croak "Either 'request' or 'path' must be supplied as arg to new"
133             unless $args{request} || $args{path};
134              
135             return $class->$orig(%args);
136             };
137              
138             sub BUILD {
139 81     81 0 6518 my $self = shift;
140 81 100       906 if ( $self->request ) {
141 16         180 $self->path;
142 16         774 $self->request_path;
143 16         599 $self->query_string;
144 16         729 $self->_clear_request;
145             }
146             }
147              
148             =head2 predicates
149              
150             The following predicate methods are defined:
151              
152             =over
153              
154             =item * has_attributes
155              
156             =item * has_title
157              
158             =back
159              
160             =head2 uri
161              
162             Returns the string URI for L and L.
163              
164             =cut
165              
166             sub uri {
167 44     44 1 2213 my $self = shift;
168 44         556 my $uri = $self->path;
169 44 100       644 $uri .= '?' . $self->query_string if $self->query_string;
170 44         495 return "$uri";
171             }
172              
173             =head2 request_uri
174              
175             Returns the string URI for L and L.
176              
177             =cut
178              
179             sub request_uri {
180 2     2 1 6 my $self = shift;
181 2         25 my $uri = $self->request_path;
182 2 50       30 $uri .= '?' . $self->query_string if $self->query_string;
183 2         44 return "$uri";
184             }
185              
186             1;