Skip to content

Commit 55955c7

Browse files
committed
Idiomatic refactor of proxies
1 parent e02c554 commit 55955c7

File tree

2 files changed

+277
-86
lines changed

2 files changed

+277
-86
lines changed

lib/CSS/Properties.rakumod

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -318,77 +318,79 @@ method !build-declarations(@style) {
318318

319319
# Accessor for a four sided value: top, left, bottom, right
320320
method !box-value(Str $prop, CArray $edges) is rw {
321-
Proxy.new(
322-
FETCH => -> $ {
323-
%!box{$prop} //= do {
324-
my $n = 0;
325-
my @bound;
326-
@bound[$n++] := self!item-value($_)
327-
for $edges.list;
328-
@bound;
329-
}
330-
},
331-
STORE => -> $, $v {
332-
with $v {
333-
# expand and assign values to child properties
334-
my @v = .isa(List) ?? .list !! $_;
335-
@v[1] //= @v[0];
336-
@v[2] //= @v[0];
337-
@v[3] //= @v[1];
338-
339-
my $n = 0;
340-
for $edges.list -> $prop {
341-
%!values{$prop} = $_
342-
with self!coerce( @v[$n++], :$prop )
343-
}
344-
}
345-
else {
346-
self.delete($prop);
321+
sub FETCH($) {
322+
%!box{$prop} //= do {
323+
my $n = 0;
324+
my @bound;
325+
@bound[$n++] := self!item-value($_)
326+
for $edges.list;
327+
@bound;
328+
}
329+
}
330+
sub STORE($, $v) {
331+
with $v {
332+
# expand and assign values to child properties
333+
my @v = .isa(List) ?? .list !! $_;
334+
@v[1] //= @v[0];
335+
@v[2] //= @v[0];
336+
@v[3] //= @v[1];
337+
338+
my $n = 0;
339+
for $edges.list -> $prop {
340+
%!values{$prop} = $_
341+
with self!coerce( @v[$n++], :$prop )
347342
}
348343
}
349-
);
344+
else {
345+
self.delete($prop);
346+
}
347+
}
348+
349+
Proxy.new: :&FETCH, :&STORE;
350350
}
351351

352352
# accessor for a structured property. e.g. font -> font-name, style...
353353
method !struct-value(Str $prop, CArray $children) is rw {
354-
Proxy.new(
355-
FETCH => -> $ {
356-
%!struct{$prop} //= do {
357-
my $n = 0;
358-
my %bound;
359-
%bound{$_} := self!lval($_)
360-
for $children.list;
361-
%bound;
362-
}
363-
},
364-
STORE => -> $, $rval {
365-
my %vals;
366-
with $rval {
367-
when Associative { %vals = .Hash; }
368-
default {
369-
with self.parse-property($prop, $_, :$!warn) -> $expr {
370-
%vals{.key} = .value
371-
for self!get-container-prop($prop, $expr);
372-
}
354+
355+
sub FETCH($) {
356+
%!struct{$prop} //= do {
357+
my $n = 0;
358+
my %bound;
359+
%bound{$_} := self!lval($_)
360+
for $children.list;
361+
%bound;
362+
}
363+
}
364+
365+
sub STORE($, $rval) {
366+
my %vals;
367+
with $rval {
368+
when Associative { %vals = .Hash; }
369+
default {
370+
with self.parse-property($prop, $_, :$!warn) -> $expr {
371+
%vals{.key} = .value
372+
for self!get-container-prop($prop, $expr);
373373
}
374374
}
375+
}
376+
else {
377+
self.delete($prop);
378+
}
379+
380+
for $children.list -> $prop {
381+
with %vals{$prop}:delete {
382+
self!lval($prop) = $_
383+
with self!coerce($_, :$prop);
384+
}
375385
else {
376386
self.delete($prop);
377387
}
378-
379-
for $children.list -> $prop {
380-
with %vals{$prop}:delete {
381-
self!lval($prop) = $_
382-
with self!coerce($_, :$prop);
383-
}
384-
else {
385-
self.delete($prop);
386-
}
387-
}
388-
note "unknown child properties of $prop: {%vals.keys.sort}"
389-
if %vals
390388
}
391-
);
389+
note "unknown child properties of $prop: {%vals.keys.sort}"
390+
if %vals
391+
}
392+
393+
Proxy.new: :&FETCH, :&STORE;
392394
}
393395

394396
# get the default for this property.
@@ -409,30 +411,29 @@ method !default-value($_) {
409411

410412
# accessor for a simple value
411413
method !item-value(Str $prop) is rw {
412-
Proxy.new(
413-
FETCH => -> $ {
414-
%!values{$prop} // self!default-value($prop);
415-
},
416-
STORE => -> $, $v {
417-
with self!coerce( $v, :$prop ) {
418-
$!calc.em = self.measure(:font-size($_))
419-
if $prop eq 'font-size';
420-
%!values{$prop} = $_;
421-
}
422-
else {
423-
self.delete($prop);
424-
}
414+
sub FETCH($) {
415+
%!values{$prop} // self!default-value($prop);
416+
}
417+
sub STORE($, $v) {
418+
with self!coerce( $v, :$prop ) {
419+
$!calc.em = self.measure(:font-size($_))
420+
if $prop eq 'font-size';
421+
%!values{$prop} = $_;
425422
}
426-
);
423+
else {
424+
self.delete($prop);
425+
}
426+
}
427+
Proxy.new: :&FETCH, :&STORE;
427428
}
428429

429430
method !child-handling(CArray $children) is rw {
430-
Proxy.new(
431-
FETCH => -> $ { [&&] $children.map: { %!handling{$_} } },
432-
STORE => -> $, Str $h {
433-
%!handling{$_} = $h
434-
for $children.list;
435-
});
431+
sub FETCH($) { [&&] $children.map: { %!handling{$_} } }
432+
sub STORE($, Str $h) {
433+
%!handling{$_} = $h
434+
for $children.list;
435+
}
436+
Proxy.new: :&FETCH, :&STORE;
436437
}
437438

438439
method !handling() { %!handling }
@@ -461,12 +462,13 @@ multi method inherited returns Seq {
461462
}
462463

463464
method !child-importance(CArray $children) is rw {
464-
Proxy.new(
465-
FETCH => -> $ { [&&] $children.map: { %!important{$_} } },
466-
STORE => -> $, Bool $v {
467-
%!important{$_} = $v
468-
for $children.list;
469-
});
465+
sub FETCH($) { [&&] $children.map: { %!important{$_} } }
466+
sub STORE($, Bool $v) {
467+
%!important{$_} = $v
468+
for $children.list;
469+
}
470+
471+
Proxy.new: :&FETCH, :&STORE;
470472
}
471473

472474
#| return True if the property has the !important attribute
@@ -610,7 +612,7 @@ multi sub to-ast($v, :$get = True) is default {
610612
}
611613

612614
#| CSS conformant inheritance from the given parent declaration list.
613-
multi method inherit(CSS::Properties:D() $css) {
615+
method inherit(CSS::Properties:D() $css) {
614616
for $css.properties -> \name {
615617
# skip unknown extension properties
616618
next if name.starts-with('-') && !self.prop-num(name).defined;

0 commit comments

Comments
 (0)