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
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

View file

@ -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,