From 1cc2015ad173e2f29b1f191574a9becd53f830da Mon Sep 17 00:00:00 2001 From: nyarla Date: Mon, 4 Mar 2024 13:26:19 +0900 Subject: [PATCH] feat(Kalaclista::Data::Entry): add new class to entry data --- lib/Kalaclista/Data/Entry.pm | 63 ++++++++++++++++++++++++++++ t/Kalaclista-Data-Entry/00_compile.t | 16 +++++++ t/Kalaclista-Data-Entry/10_common.t | 55 ++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 lib/Kalaclista/Data/Entry.pm create mode 100644 t/Kalaclista-Data-Entry/00_compile.t create mode 100644 t/Kalaclista-Data-Entry/10_common.t diff --git a/lib/Kalaclista/Data/Entry.pm b/lib/Kalaclista/Data/Entry.pm new file mode 100644 index 0000000..7ebed8e --- /dev/null +++ b/lib/Kalaclista/Data/Entry.pm @@ -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); + } +} diff --git a/t/Kalaclista-Data-Entry/00_compile.t b/t/Kalaclista-Data-Entry/00_compile.t new file mode 100644 index 0000000..f62b132 --- /dev/null +++ b/t/Kalaclista-Data-Entry/00_compile.t @@ -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; diff --git a/t/Kalaclista-Data-Entry/10_common.t b/t/Kalaclista-Data-Entry/10_common.t new file mode 100644 index 0000000..11838c7 --- /dev/null +++ b/t/Kalaclista-Data-Entry/10_common.t @@ -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;