Rename "kind" to "category"

This commit is contained in:
Alex Kotov 2023-09-30 18:10:31 +04:00
parent f67e2483d2
commit 421d6d2907
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
9 changed files with 68 additions and 68 deletions

View file

@ -52,9 +52,9 @@ while (line = $stdin.gets)
**data.except('name').transform_keys(&:to_sym), **data.except('name').transform_keys(&:to_sym),
) )
success success
when 'REGISTER_KIND' when 'REGISTER_CATEGORY'
data = JSON.parse rest data = JSON.parse rest
config.kinds.register String(data).to_sym config.categories.register String(data).to_sym
success success
when 'REGISTER_REF' when 'REGISTER_REF'
data = JSON.parse rest data = JSON.parse rest
@ -64,21 +64,21 @@ while (line = $stdin.gets)
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
success footnotes.make_ref( success footnotes.make_ref(
String(data['kind']).to_sym, String(data['category']).to_sym,
String(data['id']), String(data['id']),
).to_h ).to_h
when 'REF' when 'REF'
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
success config.repo[ success config.repo[
String(data['kind']).to_sym, String(data['category']).to_sym,
String(data['id']), String(data['id']),
].to_h ].to_h
when 'FETCH_NOTE' when 'FETCH_NOTE'
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
success footnotes.fetch_note( success footnotes.fetch_note(
String(data['kind']).to_sym, String(data['category']).to_sym,
String(data['id']), String(data['id']),
).to_h ).to_h
when 'RENDER_FOOTNOTES' when 'RENDER_FOOTNOTES'

View file

@ -6,8 +6,8 @@ require 'open3'
require 'pathname' require 'pathname'
require_relative 'referator/config' require_relative 'referator/config'
require_relative 'referator/config/categories'
require_relative 'referator/config/formats' require_relative 'referator/config/formats'
require_relative 'referator/config/kinds'
require_relative 'referator/config/repo' require_relative 'referator/config/repo'
require_relative 'referator/footnotes' require_relative 'referator/footnotes'
require_relative 'referator/note' require_relative 'referator/note'

View file

@ -10,7 +10,7 @@ module Referator
def freeze def freeze
formats.freeze formats.freeze
kinds.freeze categories.freeze
repo.freeze repo.freeze
super super
end end
@ -19,8 +19,8 @@ module Referator
@formats ||= Formats.new self @formats ||= Formats.new self
end end
def kinds(&) def categories(&)
@kinds ||= Kinds.new self @categories ||= Categories.new self
end end
def repo(&) def repo(&)

View file

@ -2,7 +2,7 @@
module Referator module Referator
class Config class Config
class Kinds class Categories
attr_reader :config, :names attr_reader :config, :names
def initialize(config) def initialize(config)
@ -24,7 +24,7 @@ module Referator
end end
def exists!(name) def exists!(name)
names.index name or raise 'Unknown kind' names.index name or raise 'Unknown category'
nil nil
end end

View file

@ -17,16 +17,16 @@ module Referator
def register_ref(**kwargs) def register_ref(**kwargs)
reference = Reference.new(**kwargs) reference = Reference.new(**kwargs)
key = "#{reference.kind}-#{reference.id}".freeze key = "#{reference.category}-#{reference.id}".freeze
raise 'Reference already exists' if @references.key? key raise 'Reference already exists' if @references.key? key
@references[key] = reference @references[key] = reference
nil nil
end end
def [](kind, id) def [](category, id)
config.kinds.exists! kind config.categories.exists! category
key = "#{kind}-#{id}".freeze key = "#{category}-#{id}".freeze
@references[key].tap do |reference| @references[key].tap do |reference|
raise 'Invalid reference' if reference.nil? raise 'Invalid reference' if reference.nil?
end end

View file

@ -10,24 +10,24 @@ module Referator
@references = {} @references = {}
end end
def make_ref(kind, id) def make_ref(category, id)
config.kinds.exists! kind config.categories.exists! category
@references[kind] ||= [] @references[category] ||= []
@references[kind].each do |reference| @references[category].each do |reference|
return reference if reference.kind == kind && reference.id == id return reference if reference.category == category && reference.id == id
end end
reference = config.repo[kind, id] reference = config.repo[category, id]
@references[kind] << reference @references[category] << reference
reference reference
end end
def fetch_note(kind, id) def fetch_note(category, id)
config.kinds.exists! kind config.categories.exists! category
index = 0 index = 0
config.kinds.names.each do |cur_kind| config.categories.names.each do |cur_category|
(@references[cur_kind] ||= []).each do |reference| (@references[cur_category] ||= []).each do |reference|
index += 1 index += 1
next if cur_kind != kind next if cur_category != category
return Note.new index, reference if reference.id == id return Note.new index, reference if reference.id == id
end end
@ -52,10 +52,10 @@ module Referator
def notes def notes
index = 0 index = 0
config.kinds.names.to_h do |kind| config.categories.names.to_h do |category|
[ [
kind, category,
(@references[kind] ||= []) (@references[category] ||= [])
.map { |reference| Note.new (index += 1), reference } .map { |reference| Note.new (index += 1), reference }
.map(&:to_h) .map(&:to_h)
.freeze, .freeze,

View file

@ -2,21 +2,21 @@
module Referator module Referator
class Reference class Reference
attr_reader :kind, :id, :slug, :data attr_reader :category, :id, :slug, :data
def initialize(kind:, id:, slug:, **kwargs) def initialize(category:, id:, slug:, **kwargs)
self.kind = kind self.category = category
self.id = id self.id = id
self.slug = slug self.slug = slug
self.data = kwargs self.data = kwargs
end end
def to_h def to_h
@to_h ||= data.merge(kind:, id:, slug:, anchor:, fragment:).freeze @to_h ||= data.merge(category:, id:, slug:, anchor:, fragment:).freeze
end end
def anchor def anchor
@anchor ||= "#{kind}-#{slug}".freeze @anchor ||= "#{category}-#{slug}".freeze
end end
def fragment def fragment
@ -25,9 +25,9 @@ module Referator
private private
def kind=(kind) def category=(category)
@kind = String(kind).to_sym.tap do |new_kind| @category = String(category).to_sym.tap do |new_category|
raise 'Invalid kind' unless NAME_RE.match? new_kind raise 'Invalid category' unless NAME_RE.match? new_category
end end
end end

50
test.rb
View file

@ -9,13 +9,13 @@ EXE = File.expand_path('exe/referator', __dir__).freeze
RAW_REFS = { RAW_REFS = {
self: { self: {
foo: { foo: {
'kind' => 'self', 'category' => 'self',
'id' => '/blog/foo', 'id' => '/blog/foo',
'slug' => 'blog-foo', 'slug' => 'blog-foo',
'text' => 'Foo', 'text' => 'Foo',
}.freeze, }.freeze,
bar: { bar: {
'kind' => 'self', 'category' => 'self',
'id' => '/blog/bar', 'id' => '/blog/bar',
'slug' => 'blog-bar', 'slug' => 'blog-bar',
'text' => 'Bar', 'text' => 'Bar',
@ -23,14 +23,14 @@ RAW_REFS = {
}.freeze, }.freeze,
link: { link: {
example_com: { example_com: {
'kind' => 'link', 'category' => 'link',
'id' => 'example_com', 'id' => 'example_com',
'slug' => 'example-com', 'slug' => 'example-com',
'url' => 'https://example.com', 'url' => 'https://example.com',
'text' => 'Example Domain', 'text' => 'Example Domain',
}.freeze, }.freeze,
causa_arcana_com: { causa_arcana_com: {
'kind' => 'link', 'category' => 'link',
'id' => 'causa_arcana_com', 'id' => 'causa_arcana_com',
'slug' => 'causa-arcana-com', 'slug' => 'causa-arcana-com',
'url' => 'https://causa-arcana.com', 'url' => 'https://causa-arcana.com',
@ -87,12 +87,12 @@ def cmd(name, data)
end end
end end
################# #####################
# REGISTER_KIND # # REGISTER_CATEGORY #
################# #####################
cmd :REGISTER_KIND, :self cmd :REGISTER_CATEGORY, :self
cmd :REGISTER_KIND, :link cmd :REGISTER_CATEGORY, :link
################### ###################
# REGISTER_FORMAT # # REGISTER_FORMAT #
@ -123,19 +123,19 @@ cmd :REGISTER_REF, RAW_REFS[:link][:causa_arcana_com]
# REF # # REF #
####### #######
cmd :REF, { kind: :self, id: '/blog/foo' } do |result| cmd :REF, { category: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo] raise unless result == REFS[:self][:foo]
end end
cmd :REF, { kind: :self, id: '/blog/bar' } do |result| cmd :REF, { category: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar] raise unless result == REFS[:self][:bar]
end end
cmd :REF, { kind: :link, id: :example_com } do |result| cmd :REF, { category: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com] raise unless result == REFS[:link][:example_com]
end end
cmd :REF, { kind: :link, id: :causa_arcana_com } do |result| cmd :REF, { category: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com] raise unless result == REFS[:link][:causa_arcana_com]
end end
@ -143,19 +143,19 @@ end
# MAKE_REF # # MAKE_REF #
############ ############
cmd :MAKE_REF, { kind: :self, id: '/blog/foo' } do |result| cmd :MAKE_REF, { category: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo] raise unless result == REFS[:self][:foo]
end end
cmd :MAKE_REF, { kind: :link, id: :example_com } do |result| cmd :MAKE_REF, { category: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com] raise unless result == REFS[:link][:example_com]
end end
cmd :MAKE_REF, { kind: :self, id: '/blog/bar' } do |result| cmd :MAKE_REF, { category: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar] raise unless result == REFS[:self][:bar]
end end
cmd :MAKE_REF, { kind: :link, id: :causa_arcana_com } do |result| cmd :MAKE_REF, { category: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com] raise unless result == REFS[:link][:causa_arcana_com]
end end
@ -163,19 +163,19 @@ end
# FETCH_NOTE # # FETCH_NOTE #
############## ##############
cmd :FETCH_NOTE, { kind: :self, id: '/blog/foo' } do |result| cmd :FETCH_NOTE, { category: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo].merge('index' => 1) raise unless result == REFS[:self][:foo].merge('index' => 1)
end end
cmd :FETCH_NOTE, { kind: :self, id: '/blog/bar' } do |result| cmd :FETCH_NOTE, { category: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar].merge('index' => 2) raise unless result == REFS[:self][:bar].merge('index' => 2)
end end
cmd :FETCH_NOTE, { kind: :link, id: :example_com } do |result| cmd :FETCH_NOTE, { category: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com].merge('index' => 3) raise unless result == REFS[:link][:example_com].merge('index' => 3)
end end
cmd :FETCH_NOTE, { kind: :link, id: :causa_arcana_com } do |result| cmd :FETCH_NOTE, { category: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4) raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4)
end end
@ -217,7 +217,7 @@ cmd :RENDER_FOOTNOTES, :json do |result|
"self": [ "self": [
{ {
"index": 1, "index": 1,
"kind": "self", "category": "self",
"id": "/blog/foo", "id": "/blog/foo",
"slug": "blog-foo", "slug": "blog-foo",
"anchor": "self-blog-foo", "anchor": "self-blog-foo",
@ -227,7 +227,7 @@ cmd :RENDER_FOOTNOTES, :json do |result|
}, },
{ {
"index": 2, "index": 2,
"kind": "self", "category": "self",
"id": "/blog/bar", "id": "/blog/bar",
"slug": "blog-bar", "slug": "blog-bar",
"anchor": "self-blog-bar", "anchor": "self-blog-bar",
@ -239,7 +239,7 @@ cmd :RENDER_FOOTNOTES, :json do |result|
"link": [ "link": [
{ {
"index": 3, "index": 3,
"kind": "link", "category": "link",
"id": "example_com", "id": "example_com",
"slug": "example-com", "slug": "example-com",
"anchor": "link-example-com", "anchor": "link-example-com",
@ -249,7 +249,7 @@ cmd :RENDER_FOOTNOTES, :json do |result|
}, },
{ {
"index": 4, "index": 4,
"kind": "link", "category": "link",
"id": "causa_arcana_com", "id": "causa_arcana_com",
"slug": "causa-arcana-com", "slug": "causa-arcana-com",
"anchor": "link-causa-arcana-com", "anchor": "link-causa-arcana-com",

View file

@ -3,7 +3,7 @@
<%- notes['self'].each_with_index do |note, index| -%> <%- notes['self'].each_with_index do |note, index| -%>
{ {
"index": <%= Integer(note['index']).to_json %>, "index": <%= Integer(note['index']).to_json %>,
"kind": "self", "category": "self",
"id": <%= String(note['id']).to_json %>, "id": <%= String(note['id']).to_json %>,
"slug": <%= String(note['slug']).to_json %>, "slug": <%= String(note['slug']).to_json %>,
"anchor": <%= String(note['anchor']).to_json %>, "anchor": <%= String(note['anchor']).to_json %>,
@ -17,7 +17,7 @@
<%- notes['link'].each_with_index do |note, index| -%> <%- notes['link'].each_with_index do |note, index| -%>
{ {
"index": <%= Integer(note['index']).to_json %>, "index": <%= Integer(note['index']).to_json %>,
"kind": "link", "category": "link",
"id": <%= String(note['id']).to_json %>, "id": <%= String(note['id']).to_json %>,
"slug": <%= String(note['slug']).to_json %>, "slug": <%= String(note['slug']).to_json %>,
"anchor": <%= String(note['anchor']).to_json %>, "anchor": <%= String(note['anchor']).to_json %>,