blib/lib/Uniform/HTMX.pm

filter: Statementstmt 75.7% 97 / 128 Branchbran 52.7% 19 / 36 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 68.2% 28 / 41 Podpod 100% 23 / 23 totaltotal 71.7% 170 / 237 CC 23 SCAR 11.2
CC
23
Cov
70%
res_refresh
12.0
_event_header
6.9
res_location
6.0
CRAP 3.1
↑ Summary
1 package Uniform::HTMX;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Uniform::Utils qw(normalize_http_headers);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.9
8 3 ▶ if (eval { require JSON::MaybeXS; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require JSON::MaybeXS; 1 } }) { } 3 0 3
9 3 JSON::MaybeXS->import('encode_json', 'decode_json');
10 } else {
11 0 require JSON::PP;
12 0 JSON::PP->import('encode_json', 'decode_json');
13 }
14 }
15  
16 3 our $VERSION = '0.16';
17  
18 # =========================================================================
19 # CONSTRUCTOR (optional — subclasses may use, extend via SUPER::new, or
20 # ignore entirely and bless their own hashref; see EXTENDING UNIFORM::HTMX)
21 # =========================================================================
22  
23 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
24 3 my ($class, %args) = @_;
25 3 my $self = bless { %args, in => {}, out => {} }, $class;
26 3 ▶ $self->{in} = $self->_normalize_headers(delete($args{in}) || {});
Condition50%1/2
delete $args{'in'} || {}
l!l
30
MC/DC0%0/1
delete $args{'in'} || {}
delete $args{'in'}
Truth table50%1/2
A
A= delete $args{'in'}
Aresult
11
00
27 3 return $self;
28 }
29  
30 # =========================================================================
31 # DRIVER SHORTCUT — build a framework integration without writing a
32 # subclass at all. See EXTENDING UNIFORM::HTMX for the full walkthrough.
33 # =========================================================================
34  
35 3 my $ANON_SEQ = 0;
36  
37 4 ▶sub driver {
Subroutine driver: called CC=3 SCAR=11.0
Pod documented
38 4 my ($class, %spec) = @_;
39  
40 4 for my $required (qw(extract apply)) {
41 croak "Uniform::HTMX->driver() requires a '$required' coderef"
42 7 ▶ unless ref($spec{$required}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec{$required} eq "CODE" 2 5 7
43 }
44  
45 # Generate a private, anonymous subclass so the two closures have a
46 # real home as ->new / ->apply, and so nothing here pollutes the
47 # Uniform::HTMX namespace itself or collides with a real driver
48 # distribution the caller may also have loaded.
49 2 my $anon_class = sprintf('Uniform::HTMX::_Anon%04d', ++$ANON_SEQ);
50  
51 {
52 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 push @{"${anon_class}::ISA"}, $class;
54  
55 2 my $base_new = $class->can('new');
56  
57 2 ▶ *{"${anon_class}::new"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 3 my ($inner_class, $scope) = @_;
59 3 my %raw_headers = $spec{extract}->($scope);
60 # Resolved via can() rather than SUPER:: — a closure installed
61 # via typeglob is compiled in Uniform::HTMX's package, so
62 # SUPER:: would incorrectly resolve against *its* @ISA rather
63 # than $anon_class's.
64 3 return $base_new->($inner_class, in => \%raw_headers, _scope => $scope);
65 2 };
66  
67 2 ▶ *{"${anon_class}::apply"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 2 my ($self, @rest) = @_;
69 2 return $spec{apply}->($self, $self->{_scope}, $self->_out, @rest);
70 2 };
71 }
72  
73 # Return a plain constructor closure — the author never needs to know
74 # $anon_class exists, or that this is a subclass at all.
75 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 3 my ($scope) = @_;
77 3 return $anon_class->new($scope);
78 2 };
79 }
80  
81 # =========================================================================
82 # OVERRIDABLE HOOKS
83 #
84 # These exist so a driver subclass can customize storage/serialization
85 # behavior WITHOUT needing to reimplement any request/response method
86 # above. Everything in this file reads/writes state exclusively through
87 # these hooks rather than touching $self->{in}/{out} or JSON functions
88 # directly, so overriding a hook here changes behavior everywhere.
89 # =========================================================================
90  
91 # Storage for normalized inbound headers. Override if headers should be
92 # read lazily (e.g. pulled from a framework context object on demand)
93 # instead of eagerly normalized in new().
94 11 ▶sub _in { $_[0]->{in} ||= {} }
Subroutine _in: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"in"} ||= {}
l!l
110
MC/DC0%0/1
$_[0]{"in"} ||= {}
$_[0]{"in"}
Truth table50%1/2
A
A= $_[0]{"in"}
Aresult
11
00
95  
96 # Storage for outbound headers to be applied by the driver's apply().
97 8 ▶sub _out { $_[0]->{out} ||= {} }
Subroutine _out: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"out"} ||= {}
l!l
80
MC/DC0%0/1
$_[0]{"out"} ||= {}
$_[0]{"out"}
Truth table50%1/2
A
A= $_[0]{"out"}
Aresult
11
00
98  
99 # JSON encode/decode hooks. Override to swap serializer, force canonical
100 # key order, pretty-print for debugging, etc.
101 2 ▶sub _encode_json { my ($self, $data) = @_; return encode_json($data) }
Subroutine _encode_json: called CC=1 SCAR=0.0
102 1 ▶sub _decode_json { my ($self, $json) = @_; return decode_json($json) }
Subroutine _decode_json: called CC=1 SCAR=0.0
103  
104 # Internal normalization utility available to all independent subclasses
105 10 ▶sub _normalize_headers {
Subroutine _normalize_headers: called CC=1 SCAR=0.0
106 10 my ($self, $hash) = @_;
107 # Delegate cleanly to your core ecosystem utility!
108 10 return normalize_http_headers($hash);
109 }
110  
111 11 ▶sub _request_header {
Subroutine _request_header: called CC=2 SCAR=7.0
112 11 my ($self, $name) = @_;
113 11 my $in = $self->_in;
114 11 ▶ return unless ref($in) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $in eq "HASH" 0 11 11
115 11 return $in->{lc $name};
116 }
117  
118 4 ▶sub _true_header {
Subroutine _true_header: called CC=3 SCAR=11.0
119 4 my ($self, $name) = @_;
120 4 my $value = $self->_request_header($name);
121 4 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
122 4 $value =~ s/^\s+|\s+$//g;
123 4 ▶ return lc($value) eq 'true' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $value eq 'true' ? : 3 1 4
124 }
125  
126 7 ▶sub _response_header {
Subroutine _response_header: called CC=4 SCAR=14.0
127 7 my ($self, $name, $value) = @_;
128 7 ▶ croak "$name value must be defined" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
129 7 ▶ croak "$name value must be a plain scalar" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 7 7
130 7 ▶ croak "$name value must not contain a newline" if $value =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\r\n]/ 2 5 7
131 5 $self->_out->{$name} = "$value";
132 5 return $self;
133 }
134  
135 3 ▶sub _event_header {
Subroutine _event_header: called CC=6 SCAR=19.3
136 3 my ($self, $header, $event, $params) = @_;
137 3 ▶ croak "Event name must be defined" unless defined $event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event 0 3 3
138 3 ▶ croak "Event name must be a plain scalar" if ref $event;
Branch: 1 branch
BranchTrueFalseTotal
if ref $event 0 3 3
139 3 ▶ croak "Event name must not be empty" unless length $event;
Branch: 1 branch
BranchTrueFalseTotal
unless length $event 0 3 3
140 3 ▶ croak "Event name must not contain a newline" if $event =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $event =~ /[\r\n]/ 0 3 3
141  
142 3 ▶ my $value = defined($params) ? $self->_encode_json({ $event => $params }) : $event;
Branch: 1 branch
BranchTrueFalseTotal
defined $params ? : 3 0 3
143 3 return $self->_response_header($header, $value);
144 }
145  
146 # =========================================================================
147 # REQUEST INSPECTION METHODS
148 # =========================================================================
149  
150 3 ▶sub is_htmx {
Subroutine is_htmx: called CC=1 SCAR=0.0
Pod documented
151 3 my ($self) = @_;
152 3 return $self->_true_header('hx-request');
153 }
154  
155 1 ▶sub is_boosted {
Subroutine is_boosted: called CC=1 SCAR=0.0
Pod documented
156 1 my ($self) = @_;
157 1 return $self->_true_header('hx-boosted');
158 }
159  
160 0 ▶sub is_history_restore {
Subroutine is_history_restore: not called CC=1 SCAR=6.9
Pod documented
161 0 my ($self) = @_;
162 0 return $self->_true_header('hx-history-restore-request');
163 }
164  
165 0 ▶sub current_url {
Subroutine current_url: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($self) = @_;
167 0 return $self->_request_header('hx-current-url');
168 }
169  
170 0 ▶sub prompt {
Subroutine prompt: not called CC=1 SCAR=6.9
Pod documented
171 0 my ($self) = @_;
172 0 return $self->_request_header('hx-prompt');
173 }
174  
175 4 ▶sub target {
Subroutine target: called CC=1 SCAR=0.0
Pod documented
176 4 my ($self) = @_;
177 4 return $self->_request_header('hx-target');
178 }
179  
180 1 ▶sub trigger_id {
Subroutine trigger_id: called CC=1 SCAR=0.0
Pod documented
181 1 my ($self) = @_;
182 1 return $self->_request_header('hx-trigger');
183 }
184  
185 0 ▶sub trigger_name {
Subroutine trigger_name: not called CC=1 SCAR=6.9
Pod documented
186 0 my ($self) = @_;
187 0 return $self->_request_header('hx-trigger-name');
188 }
189  
190 2 ▶sub trigger_event {
Subroutine trigger_event: called CC=3 SCAR=11.0
Pod documented
191 2 my ($self) = @_;
192 2 my $val = $self->_request_header('hx-trigger');
193 2 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 2 2
194  
195 # If it looks like a JSON string sent from an htmx event trigger, decode it safely
196 2 ▶ if ($val =~ /^\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\s*[\{\[]/) 1 1 2
197 1 return eval { $self->_decode_json($val) };
198 }
199 1 return $val;
200 }
201  
202 # =========================================================================
203 # RESPONSE MANIPULATION METHODS
204 # =========================================================================
205  
206 1 ▶sub res_retarget {
Subroutine res_retarget: called CC=1 SCAR=0.0
Pod documented
207 1 my ($self, $selector) = @_;
208 1 return $self->_response_header('HX-Retarget', $selector);
209 }
210  
211 3 ▶sub res_reswap {
Subroutine res_reswap: called CC=1 SCAR=0.0
Pod documented
212 3 my ($self, $strategy) = @_;
213 3 return $self->_response_header('HX-Reswap', $strategy);
214 }
215  
216 0 ▶sub res_reselect {
Subroutine res_reselect: not called CC=1 SCAR=6.9
Pod documented
217 0 my ($self, $selector) = @_;
218 0 return $self->_response_header('HX-Reselect', $selector);
219 }
220  
221 0 ▶sub res_location {
Subroutine res_location: not called CC=2 SCAR=17.9
Pod documented
222 0 my ($self, $location) = @_;
223 0 ▶ if (ref($location) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $location eq "HASH") 0 0 0
224 0 return $self->_response_header('HX-Location', $self->_encode_json($location));
225 }
226 0 return $self->_response_header('HX-Location', $location);
227 }
228  
229 0 ▶sub res_push_url {
Subroutine res_push_url: not called CC=1 SCAR=6.9
Pod documented
230 0 my ($self, $url) = @_;
231 0 return $self->_response_header('HX-Push-Url', $url);
232 }
233  
234 0 ▶sub res_replace_url {
Subroutine res_replace_url: not called CC=1 SCAR=6.9
Pod documented
235 0 my ($self, $url) = @_;
236 0 return $self->_response_header('HX-Replace-Url', $url);
237 }
238  
239 0 ▶sub res_redirect {
Subroutine res_redirect: not called CC=1 SCAR=6.9
Pod documented
240 0 my ($self, $url) = @_;
241 0 return $self->_response_header('HX-Redirect', $url);
242 }
243  
244 0 ▶sub res_refresh {
Subroutine res_refresh: not called CC=3 SCAR=24.8
Pod documented
245 0 my ($self, $refresh) = @_;
246 0 ▶ $refresh = 1 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 0 0
247 0 ▶ return $self->_response_header('HX-Refresh', $refresh ? 'true' : 'false');
Branch: 1 branch
BranchTrueFalseTotal
$refresh ? : 0 0 0
248 }
249  
250 0 ▶sub res_trigger_name {
Subroutine res_trigger_name: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($self, $name) = @_;
252 0 return $self->_response_header('HX-Trigger-Name', $name);
253 }
254  
255 3 ▶sub res_trigger {
Subroutine res_trigger: called CC=1 SCAR=0.0
Pod documented
256 3 my ($self, $event, $params) = @_;
257 3 return $self->_event_header('HX-Trigger', $event, $params);
258 }
259  
260 0 ▶sub res_trigger_after_settle {
Subroutine res_trigger_after_settle: not called CC=1 SCAR=6.9
Pod documented
261 0 my ($self, $event, $params) = @_;
262 0 return $self->_event_header('HX-Trigger-After-Settle', $event, $params);
263 }
264  
265 0 ▶sub res_trigger_after_swap {
Subroutine res_trigger_after_swap: not called CC=1 SCAR=6.9
Pod documented
266 0 my ($self, $event, $params) = @_;
267 0 return $self->_event_header('HX-Trigger-After-Swap', $event, $params);
268 }
269  
270  
271  
272 3 1;
273  
274 __END__
↑ Summary