URL are always present

This commit is contained in:
Alex Kotov 2024-09-11 08:50:03 +04:00
parent c10668a64f
commit cfae23580a
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 11 additions and 3 deletions

View file

@ -2,17 +2,18 @@
module Referator module Referator
class Reference 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.category = category
self.id = id self.id = id
self.slug = slug self.slug = slug
self.url = url
self.data = kwargs self.data = kwargs
end end
def to_h def to_h
@to_h ||= data.merge(category:, id:, slug:).freeze @to_h ||= data.merge(category:, id:, slug:, url:).freeze
end end
private private
@ -29,6 +30,11 @@ module Referator
@slug = Referator.validate_slug! String slug @slug = Referator.validate_slug! String slug
end end
def url=(url)
# TODO: validate URL
@url = String(url).freeze unless url.nil?
end
def data=(data) def data=(data)
@data = Hash(data).transform_keys { |key| String(key).to_sym }.freeze @data = Hash(data).transform_keys { |key| String(key).to_sym }.freeze
end end

View file

@ -12,12 +12,14 @@ RAW_REFS = {
'category' => 'self', 'category' => 'self',
'id' => '/blog/foo', 'id' => '/blog/foo',
'slug' => 'blog-foo', 'slug' => 'blog-foo',
'url' => nil,
'text' => 'Foo', 'text' => 'Foo',
}.freeze, }.freeze,
bar: { bar: {
'category' => 'self', 'category' => 'self',
'id' => '/blog/bar', 'id' => '/blog/bar',
'slug' => 'blog-bar', 'slug' => 'blog-bar',
'url' => nil,
'text' => 'Bar', 'text' => 'Bar',
}.freeze, }.freeze,
}.freeze, }.freeze,