From dbda041877b31743ba3943c62ad6bea82d517135 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 30 Sep 2023 04:27:26 +0400 Subject: [PATCH] Change ordering --- exe/referator | 11 ++++++-- lib/referator/footnotes.rb | 39 +++++++++++++++++++------ lib/referator/note.rb | 25 ++-------------- test.rb | 58 ++++++++++++++++++++++++++------------ 4 files changed, 81 insertions(+), 52 deletions(-) diff --git a/exe/referator b/exe/referator index 082e0dd..6bec210 100755 --- a/exe/referator +++ b/exe/referator @@ -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 diff --git a/lib/referator/footnotes.rb b/lib/referator/footnotes.rb index a272c8a..375a752 100644 --- a/lib/referator/footnotes.rb +++ b/lib/referator/footnotes.rb @@ -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 diff --git a/lib/referator/note.rb b/lib/referator/note.rb index 18176af..70ac644 100644 --- a/lib/referator/note.rb +++ b/lib/referator/note.rb @@ -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? diff --git a/test.rb b/test.rb index 7cc1362..d21d293 100755 --- a/test.rb +++ b/test.rb @@ -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

Self references

@@ -169,7 +191,7 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result|
  • Foo
  • -
  • +
  • Bar
  • @@ -177,7 +199,7 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result|

    Link references

      -