From cfae23580a1de442108876099e651f96047fea0e Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 11 Sep 2024 08:50:03 +0400 Subject: [PATCH] URL are always present --- lib/referator/reference.rb | 12 +++++++++--- test.rb | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/referator/reference.rb b/lib/referator/reference.rb index 1708ddd..2a70256 100644 --- a/lib/referator/reference.rb +++ b/lib/referator/reference.rb @@ -2,17 +2,18 @@ module Referator class Reference - attr_reader :category, :id, :slug, :data + attr_reader :category, :id, :slug, :url, :data - def initialize(category:, id:, slug:, **kwargs) + def initialize(category:, id:, slug:, url: nil, **kwargs) self.category = category self.id = id self.slug = slug + self.url = url self.data = kwargs end def to_h - @to_h ||= data.merge(category:, id:, slug:).freeze + @to_h ||= data.merge(category:, id:, slug:, url:).freeze end private @@ -29,6 +30,11 @@ module Referator @slug = Referator.validate_slug! String slug end + def url=(url) + # TODO: validate URL + @url = String(url).freeze unless url.nil? + end + def data=(data) @data = Hash(data).transform_keys { |key| String(key).to_sym }.freeze end diff --git a/test.rb b/test.rb index 0319dfd..f6c1312 100755 --- a/test.rb +++ b/test.rb @@ -12,12 +12,14 @@ RAW_REFS = { 'category' => 'self', 'id' => '/blog/foo', 'slug' => 'blog-foo', + 'url' => nil, 'text' => 'Foo', }.freeze, bar: { 'category' => 'self', 'id' => '/blog/bar', 'slug' => 'blog-bar', + 'url' => nil, 'text' => 'Bar', }.freeze, }.freeze,