URL are always present
This commit is contained in:
parent
c10668a64f
commit
cfae23580a
2 changed files with 11 additions and 3 deletions
|
@ -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
|
||||
|
|
2
test.rb
2
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,
|
||||
|
|
Loading…
Reference in a new issue