diff --git a/lib/referator/machine.rb b/lib/referator/machine.rb index 6f2dde1..3c8b3f1 100644 --- a/lib/referator/machine.rb +++ b/lib/referator/machine.rb @@ -12,60 +12,10 @@ module Referator 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 + method_name = "do_#{command.name}" + raise 'Invalid command' unless respond_to? method_name, true + + send method_name, command end private @@ -77,5 +27,83 @@ module Referator @workdir = workdir end + + ################## + # Administrative # + ################## + + def do_exit(_command) + exit + end + + def do_ping(_command) + :pong + end + + ########## + # Config # + ########## + + def do_register_script(command) + @config.scripts.register(**command.data.transform_keys(&:to_sym)) + nil + end + + def do_register_category(command) + @config.categories.register command.data + nil + end + + def do_register_format(command) + @config.formats.register(**command.data.transform_keys(&:to_sym)) + nil + end + + def do_register_ref(command) + @config.repo.register_ref(**command.data.transform_keys(&:to_sym)) + nil + end + + ######## + # Info # + ######## + + def do_ref(command) + @config.freeze + @config.repo[command.data['category'], command.data['id']].to_h + end + + ############ + # Creation # + ############ + + def do_make_ref(command) + @config.freeze + @footnotes.make_ref(command.data['category'], command.data['id']).to_h + end + + ############# + # Rendering # + ############# + + def do_fetch_footnotes(_command) + @config.freeze + @footnotes.fetch_footnotes + end + + def do_fetch_note(command) + @config.freeze + @footnotes.fetch_note(command.data['category'], command.data['id']).to_h + end + + def do_render_footnotes(command) + @config.freeze + String @footnotes.render_footnotes command.data + end + + def do_render_cite_sup(command) + @config.freeze + String @footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym)) + end end end