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),
)
success
when 'REGISTER_KIND'
when 'REGISTER_CATEGORY'
data = JSON.parse rest
config.kinds.register String(data).to_sym
config.categories.register String(data).to_sym
success
when 'REGISTER_REF'
data = JSON.parse rest
@ -64,21 +64,21 @@ while (line = $stdin.gets)
config.freeze
data = JSON.parse rest
success footnotes.make_ref(
String(data['kind']).to_sym,
String(data['category']).to_sym,
String(data['id']),
).to_h
when 'REF'
config.freeze
data = JSON.parse rest
success config.repo[
String(data['kind']).to_sym,
String(data['category']).to_sym,
String(data['id']),
].to_h
when 'FETCH_NOTE'
config.freeze
data = JSON.parse rest
success footnotes.fetch_note(
String(data['kind']).to_sym,
String(data['category']).to_sym,
String(data['id']),
).to_h
when 'RENDER_FOOTNOTES'

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

50
test.rb
View file

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

View file

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