Skip to content

Commit

Permalink
feat(Kalaclista::Data::Entry): add new class to entry data
Browse files Browse the repository at this point in the history
  • Loading branch information
nyarla committed Mar 4, 2024
1 parent e56e825 commit 1cc2015
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/Kalaclista/Data/Entry.pm
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);
}
}
16 changes: 16 additions & 0 deletions t/Kalaclista-Data-Entry/00_compile.t
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;
55 changes: 55 additions & 0 deletions t/Kalaclista-Data-Entry/10_common.t
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;

Check failure on line 31 in t/Kalaclista-Data-Entry/10_common.t

View workflow job for this annotation

GitHub Actions / test

Built-in function 'builtin::false' is experimental
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,

Check failure on line 43 in t/Kalaclista-Data-Entry/10_common.t

View workflow job for this annotation

GitHub Actions / test

Built-in function 'builtin::true' is experimental
meta => {
foo => 'baz',
baz => 'foo',
}
);

is $entry->title, 'hello, new world!';
is $entry->draft, true;

Check failure on line 51 in t/Kalaclista-Data-Entry/10_common.t

View workflow job for this annotation

GitHub Actions / test

Built-in function 'builtin::true' is experimental
is $entry->meta, { foo => 'baz', bar => 'baz', baz => 'foo' };
};

done_testing;

0 comments on commit 1cc2015

Please sign in to comment.