From 58ebf111cfdae74aae76d8998f1d8505b3675551 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 23 Oct 2023 21:08:23 +0400 Subject: [PATCH] Add class Referator::Machine --- exe/referator | 63 ++----------------------------- lib/referator.rb | 1 + lib/referator/machine.rb | 81 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 lib/referator/machine.rb diff --git a/exe/referator b/exe/referator index de270cd..3553108 100755 --- a/exe/referator +++ b/exe/referator @@ -31,70 +31,13 @@ workdir = ARGV.first workdir = Dir.pwd if String(workdir).strip.empty? workdir = File.expand_path(workdir).freeze -config = Referator::Config.new workdir -footnotes = Referator::Footnotes.new config +machine = Referator::Machine.new workdir while (line = $stdin.gets) begin command = Referator::Command.parse line - case command.name - ################## - # Administrative # - ################## - when :exit - success - exit - when :ping - success :pong - ########## - # Config # - ########## - when :register_script - config.scripts.register(**command.data.transform_keys(&:to_sym)) - success - when :register_category - config.categories.register command.data - success - when :register_format - config.formats.register(**command.data.transform_keys(&:to_sym)) - success - when :register_ref - config.repo.register_ref(**command.data.transform_keys(&:to_sym)) - success - ######## - # Info # - ######## - when :ref - config.freeze - success config.repo[command.data['category'], - command.data['id']].to_h - ############ - # Creation # - ############ - when :make_ref - config.freeze - success footnotes.make_ref(command.data['category'], - command.data['id']).to_h - ############# - # Rendering # - ############# - when :fetch_footnotes - config.freeze - success footnotes.fetch_footnotes - when :fetch_note - config.freeze - success footnotes.fetch_note(command.data['category'], - command.data['id']).to_h - when :render_footnotes - config.freeze - success String footnotes.render_footnotes command.data - when :render_cite_sup - config.freeze - success String \ - footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym)) - else - raise 'Invalid command' - end + result = machine.call command + success result rescue => e failure serialize_err e end diff --git a/lib/referator.rb b/lib/referator.rb index abd57f5..6ff1a36 100644 --- a/lib/referator.rb +++ b/lib/referator.rb @@ -12,6 +12,7 @@ require_relative 'referator/config/formats' require_relative 'referator/config/repo' require_relative 'referator/config/scripts' require_relative 'referator/footnotes' +require_relative 'referator/machine' require_relative 'referator/note' require_relative 'referator/reference' require_relative 'referator/script' diff --git a/lib/referator/machine.rb b/lib/referator/machine.rb new file mode 100644 index 0000000..6f2dde1 --- /dev/null +++ b/lib/referator/machine.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +module Referator + class Machine + attr_reader :workdir + + def initialize(workdir) + self.workdir = workdir + + @config = Config.new self.workdir + @footnotes = Footnotes.new @config + end + + def call(command) + case command.name + ################## + # Administrative # + ################## + when :exit + exit + when :ping + :pong + ########## + # Config # + ########## + when :register_script + @config.scripts.register(**command.data.transform_keys(&:to_sym)) + nil + when :register_category + @config.categories.register command.data + nil + when :register_format + @config.formats.register(**command.data.transform_keys(&:to_sym)) + nil + when :register_ref + @config.repo.register_ref(**command.data.transform_keys(&:to_sym)) + nil + ######## + # Info # + ######## + when :ref + @config.freeze + @config.repo[command.data['category'], command.data['id']].to_h + ############ + # Creation # + ############ + when :make_ref + @config.freeze + @footnotes.make_ref(command.data['category'], command.data['id']).to_h + ############# + # Rendering # + ############# + when :fetch_footnotes + @config.freeze + @footnotes.fetch_footnotes + when :fetch_note + @config.freeze + @footnotes.fetch_note(command.data['category'], command.data['id']).to_h + when :render_footnotes + @config.freeze + String @footnotes.render_footnotes command.data + when :render_cite_sup + @config.freeze + String \ + @footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym)) + else + raise 'Invalid command' + end + end + + private + + def workdir=(workdir) + workdir = Pathname.new(workdir).expand_path.freeze + raise 'Expected absolute path' unless workdir.absolute? + raise 'Expected existing directory' unless workdir.directory? + + @workdir = workdir + end + end +end