From 421d6d290718334c1066cd6139ee75b173cb6f7b Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 30 Sep 2023 18:10:31 +0400 Subject: [PATCH] Rename "kind" to "category" --- exe/referator | 10 ++-- lib/referator.rb | 2 +- lib/referator/config.rb | 6 +-- .../config/{kinds.rb => categories.rb} | 4 +- lib/referator/config/repo.rb | 8 +-- lib/referator/footnotes.rb | 30 +++++------ lib/referator/reference.rb | 22 ++++---- test.rb | 50 +++++++++---------- test/footnotes.json.erb | 4 +- 9 files changed, 68 insertions(+), 68 deletions(-) rename lib/referator/config/{kinds.rb => categories.rb} (92%) diff --git a/exe/referator b/exe/referator index 6bec210..f99c522 100755 --- a/exe/referator +++ b/exe/referator @@ -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' diff --git a/lib/referator.rb b/lib/referator.rb index 7016dc6..6529e48 100644 --- a/lib/referator.rb +++ b/lib/referator.rb @@ -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' diff --git a/lib/referator/config.rb b/lib/referator/config.rb index 8766494..d52592d 100644 --- a/lib/referator/config.rb +++ b/lib/referator/config.rb @@ -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(&) diff --git a/lib/referator/config/kinds.rb b/lib/referator/config/categories.rb similarity index 92% rename from lib/referator/config/kinds.rb rename to lib/referator/config/categories.rb index f403153..9fef650 100644 --- a/lib/referator/config/kinds.rb +++ b/lib/referator/config/categories.rb @@ -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 diff --git a/lib/referator/config/repo.rb b/lib/referator/config/repo.rb index 395aa72..f29e640 100644 --- a/lib/referator/config/repo.rb +++ b/lib/referator/config/repo.rb @@ -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 diff --git a/lib/referator/footnotes.rb b/lib/referator/footnotes.rb index 375a752..4023317 100644 --- a/lib/referator/footnotes.rb +++ b/lib/referator/footnotes.rb @@ -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, diff --git a/lib/referator/reference.rb b/lib/referator/reference.rb index 78f3ffd..64bd6e1 100644 --- a/lib/referator/reference.rb +++ b/lib/referator/reference.rb @@ -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 diff --git a/test.rb b/test.rb index d21d293..caa2b34 100755 --- a/test.rb +++ b/test.rb @@ -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", diff --git a/test/footnotes.json.erb b/test/footnotes.json.erb index 64f5859..89cec12 100644 --- a/test/footnotes.json.erb +++ b/test/footnotes.json.erb @@ -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 %>,