-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Kalaclista::Data::Entry): add new class to entry data
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use v5.38; | ||
use feature qw(class); | ||
use builtin qw(false); | ||
no warnings qw(experimental); | ||
|
||
class Kalaclista::Data::Entry { | ||
field $title : param; | ||
field $summary : param; | ||
|
||
field $section : param; | ||
|
||
field $draft : param = false; | ||
field $date : param; | ||
field $lastmod : param; | ||
|
||
field $href : param; | ||
|
||
field $src : param = ""; | ||
field $dom : param = undef; | ||
field $meta : param = {}; | ||
|
||
method title { $title } | ||
method summary { $summary } | ||
method section { $section } | ||
method draft { $draft } | ||
method date { $date } | ||
method lastmod { $lastmod } | ||
method href { $href } | ||
method src { $src } | ||
method dom { $dom } | ||
method meta { $meta } | ||
|
||
method clone { | ||
my $class = ref($self); | ||
my %overrides = @_; | ||
|
||
my %props = ( | ||
title => $title, | ||
summary => $summary, | ||
section => $section, | ||
draft => $draft, | ||
date => $date, | ||
lastmod => $lastmod, | ||
href => $href, | ||
src => $src, | ||
dom => $dom, | ||
); | ||
|
||
my %meta = $meta->%*; | ||
if ( exists $overrides{'meta'} ) { | ||
my $override = delete $overrides{'meta'}; | ||
$meta{$_} = $override->{$_} for keys $override->%*; | ||
} | ||
|
||
$props{'meta'} = {%meta}; | ||
|
||
for my $prop ( keys %overrides ) { | ||
$props{$prop} = $overrides{$prop}; | ||
} | ||
|
||
return $class->new(%props); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test2::V0; | ||
|
||
subtest use => sub { | ||
my $success = lives { | ||
use Kalaclista::Data::Entry; | ||
}; | ||
|
||
ok $success; | ||
}; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env perl | ||
|
||
use v5.38; | ||
use builtin qw(true false); | ||
use utf8; | ||
no warnings qw(experimental); | ||
|
||
use Test2::V0; | ||
use URI::Fast; | ||
use HTML5::DOM; | ||
|
||
use Kalaclista::Data::Entry; | ||
|
||
my sub entry { | ||
return Kalaclista::Data::Entry->new( | ||
title => 'hello, world!', | ||
summary => 'this is a test entry.', | ||
section => 'posts', | ||
date => '2024-01-01T00:00:00', | ||
lastmod => '2024-02-01T00:00:00', | ||
href => URI::Fast->new('https://example.com/entry'), | ||
meta => { foo => 'bar', bar => 'baz' }, | ||
); | ||
} | ||
|
||
subtest common => sub { | ||
my $entry = entry; | ||
is $entry->title, 'hello, world!'; | ||
is $entry->summary, 'this is a test entry.'; | ||
is $entry->section, 'posts'; | ||
is $entry->draft, false; | ||
is $entry->date, '2024-01-01T00:00:00'; | ||
is $entry->lastmod, '2024-02-01T00:00:00'; | ||
is $entry->href->to_string, 'https://example.com/entry'; | ||
is $entry->dom, undef; | ||
is $entry->src, ''; | ||
is $entry->meta, { foo => 'bar', bar => 'baz' }; | ||
}; | ||
|
||
subtest clone => sub { | ||
my $entry = entry->clone( | ||
title => 'hello, new world!', | ||
draft => true, | ||
meta => { | ||
foo => 'baz', | ||
baz => 'foo', | ||
} | ||
); | ||
|
||
is $entry->title, 'hello, new world!'; | ||
is $entry->draft, true; | ||
is $entry->meta, { foo => 'baz', bar => 'baz', baz => 'foo' }; | ||
}; | ||
|
||
done_testing; |