Change ordering

This commit is contained in:
Alex Kotov 2023-09-30 04:27:26 +04:00
parent 5944a20b56
commit dbda041877
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 81 additions and 52 deletions

View file

@ -60,10 +60,10 @@ while (line = $stdin.gets)
data = JSON.parse rest
config.repo.register_ref(**data.transform_keys(&:to_sym))
success
when 'ADD_NOTE'
when 'MAKE_REF'
config.freeze
data = JSON.parse rest
success footnotes.add_note(
success footnotes.make_ref(
String(data['kind']).to_sym,
String(data['id']),
).to_h
@ -74,6 +74,13 @@ while (line = $stdin.gets)
String(data['kind']).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['id']),
).to_h
when 'RENDER_FOOTNOTES'
config.freeze
data = JSON.parse rest

View file

@ -7,16 +7,32 @@ module Referator
def initialize(config)
self.config = config
@notes = []
@references = {}
end
def add_note(kind, id)
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
end
reference = config.repo[kind, id]
new_note = Note.new self, @notes.count + 1, reference
@notes.each { |note| return note if note.eql? new_note }
@notes << new_note
new_note
@references[kind] << reference
reference
end
def fetch_note(kind, id)
config.kinds.exists! kind
index = 0
config.kinds.names.each do |cur_kind|
(@references[cur_kind] ||= []).each do |reference|
index += 1
next if cur_kind != kind
return Note.new index, reference if reference.id == id
end
end
raise 'Unused note'
end
def render(format)
@ -34,10 +50,15 @@ module Referator
end
def notes
config.kinds.names.to_h do |name|
index = 0
config.kinds.names.to_h do |kind|
[
name,
@notes.select { |note| note.kind == name }.map(&:to_h).freeze,
kind,
(@references[kind] ||= [])
.map { |reference| Note.new (index += 1), reference }
.map(&:to_h)
.freeze,
]
end.freeze
end

View file

@ -2,14 +2,9 @@
module Referator
class Note
extend Forwardable
attr_reader :index, :reference
attr_reader :footnotes, :index, :reference
def_delegators :reference, :kind, :anchor, :fragment
def initialize(footnotes, index, reference)
self.footnotes = footnotes
def initialize(index, reference)
self.index = index
self.reference = reference
end
@ -18,24 +13,8 @@ module Referator
@to_h ||= reference.to_h.merge(index:).freeze
end
def eql?(other)
other.instance_of?(self.class) &&
footnotes == other.footnotes &&
anchor == other.anchor
end
def ==(other) = eql?(other) && index == other.index
private
def footnotes=(footnotes)
unless footnotes.instance_of? Footnotes
raise TypeError, "Expected #{Footnotes}, got #{footnotes.class}"
end
@footnotes = footnotes
end
def index=(index)
index = Integer index
raise ArgumentError, 'Invalid index' unless index.positive?

58
test.rb
View file

@ -77,7 +77,9 @@ def cmd(name, data)
@stdin.puts "#{name} #{data.to_json}"
result = @stdout.gets.chomp
if result.start_with? 'OK '
JSON.parse(result[3..]).freeze
result = JSON.parse(result[3..]).freeze
yield result if block_given?
result
elsif result.start_with? 'ERR '
raise "Error response: #{result[4..]}"
else
@ -121,39 +123,59 @@ cmd :REGISTER_REF, RAW_REFS[:link][:causa_arcana_com]
# REF #
#######
cmd(:REF, { kind: :self, id: '/blog/foo' }).tap do |result|
cmd :REF, { kind: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo]
end
cmd(:REF, { kind: :self, id: '/blog/bar' }).tap do |result|
cmd :REF, { kind: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar]
end
cmd(:REF, { kind: :link, id: :example_com }).tap do |result|
cmd :REF, { kind: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com]
end
cmd(:REF, { kind: :link, id: :causa_arcana_com }).tap do |result|
cmd :REF, { kind: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com]
end
############
# ADD_NOTE #
# MAKE_REF #
############
cmd(:ADD_NOTE, { kind: :self, id: '/blog/foo' }).tap do |result|
cmd :MAKE_REF, { kind: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo]
end
cmd :MAKE_REF, { kind: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com]
end
cmd :MAKE_REF, { kind: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar]
end
cmd :MAKE_REF, { kind: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com]
end
##############
# FETCH_NOTE #
##############
cmd :FETCH_NOTE, { kind: :self, id: '/blog/foo' } do |result|
raise unless result == REFS[:self][:foo].merge('index' => 1)
end
cmd(:ADD_NOTE, { kind: :link, id: :example_com }).tap do |result|
raise unless result == REFS[:link][:example_com].merge('index' => 2)
cmd :FETCH_NOTE, { kind: :self, id: '/blog/bar' } do |result|
raise unless result == REFS[:self][:bar].merge('index' => 2)
end
cmd(:ADD_NOTE, { kind: :self, id: '/blog/bar' }).tap do |result|
raise unless result == REFS[:self][:bar].merge('index' => 3)
cmd :FETCH_NOTE, { kind: :link, id: :example_com } do |result|
raise unless result == REFS[:link][:example_com].merge('index' => 3)
end
cmd(:ADD_NOTE, { kind: :link, id: :causa_arcana_com }).tap do |result|
cmd :FETCH_NOTE, { kind: :link, id: :causa_arcana_com } do |result|
raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4)
end
@ -161,7 +183,7 @@ end
# RENDER_FOOTNOTES #
####################
cmd(:RENDER_FOOTNOTES, :html).tap do |result|
cmd :RENDER_FOOTNOTES, :html do |result|
expected = <<~HTML
<h2>Self references</h2>
@ -169,7 +191,7 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result|
<li id="self-blog-foo" value="1">
<a href="/blog/foo.html">Foo</a>
</li>
<li id="self-blog-bar" value="3">
<li id="self-blog-bar" value="2">
<a href="/blog/bar.html">Bar</a>
</li>
</ol>
@ -177,7 +199,7 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result|
<h2>Link references</h2>
<ol>
<li id="link-example-com" value="2">
<li id="link-example-com" value="3">
<a href="https://example.com">Example Domain</a>
</li>
<li id="link-causa-arcana-com" value="4">
@ -189,7 +211,7 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result|
raise unless result == expected
end
cmd(:RENDER_FOOTNOTES, :json).tap do |result|
cmd :RENDER_FOOTNOTES, :json do |result|
expected = <<~JSON
{
"self": [
@ -204,7 +226,7 @@ cmd(:RENDER_FOOTNOTES, :json).tap do |result|
"text": "Foo"
},
{
"index": 3,
"index": 2,
"kind": "self",
"id": "/blog/bar",
"slug": "blog-bar",
@ -216,7 +238,7 @@ cmd(:RENDER_FOOTNOTES, :json).tap do |result|
],
"link": [
{
"index": 2,
"index": 3,
"kind": "link",
"id": "example_com",
"slug": "example-com",