Configure scripts separately
This commit is contained in:
parent
eba48f7315
commit
9c60167337
6 changed files with 101 additions and 31 deletions
|
@ -51,16 +51,17 @@ while (line = $stdin.gets)
|
||||||
##########
|
##########
|
||||||
# Config #
|
# Config #
|
||||||
##########
|
##########
|
||||||
|
when 'REGISTER_SCRIPT'
|
||||||
|
data = JSON.parse rest
|
||||||
|
config.scripts.register(**data.transform_keys(&:to_sym))
|
||||||
|
success
|
||||||
when 'REGISTER_CATEGORY'
|
when 'REGISTER_CATEGORY'
|
||||||
data = JSON.parse rest
|
data = JSON.parse rest
|
||||||
config.categories.register String(data).to_sym
|
config.categories.register String(data).to_sym
|
||||||
success
|
success
|
||||||
when 'REGISTER_FORMAT'
|
when 'REGISTER_FORMAT'
|
||||||
data = JSON.parse rest
|
data = JSON.parse rest
|
||||||
config.formats.register(
|
config.formats.register(**data.transform_keys(&:to_sym))
|
||||||
String(data['name']).to_sym,
|
|
||||||
**data.except('name').transform_keys(&:to_sym),
|
|
||||||
)
|
|
||||||
success
|
success
|
||||||
when 'REGISTER_REF'
|
when 'REGISTER_REF'
|
||||||
data = JSON.parse rest
|
data = JSON.parse rest
|
||||||
|
|
|
@ -9,6 +9,7 @@ require_relative 'referator/config'
|
||||||
require_relative 'referator/config/categories'
|
require_relative 'referator/config/categories'
|
||||||
require_relative 'referator/config/formats'
|
require_relative 'referator/config/formats'
|
||||||
require_relative 'referator/config/repo'
|
require_relative 'referator/config/repo'
|
||||||
|
require_relative 'referator/config/scripts'
|
||||||
require_relative 'referator/footnotes'
|
require_relative 'referator/footnotes'
|
||||||
require_relative 'referator/note'
|
require_relative 'referator/note'
|
||||||
require_relative 'referator/reference'
|
require_relative 'referator/reference'
|
||||||
|
|
|
@ -9,12 +9,17 @@ module Referator
|
||||||
end
|
end
|
||||||
|
|
||||||
def freeze
|
def freeze
|
||||||
|
scripts.freeze
|
||||||
categories.freeze
|
categories.freeze
|
||||||
formats.freeze
|
formats.freeze
|
||||||
repo.freeze
|
repo.freeze
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def scripts
|
||||||
|
@scripts ||= Scripts.new self
|
||||||
|
end
|
||||||
|
|
||||||
def categories
|
def categories
|
||||||
@categories ||= Categories.new self
|
@categories ||= Categories.new self
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,40 +3,40 @@
|
||||||
module Referator
|
module Referator
|
||||||
class Config
|
class Config
|
||||||
class Formats
|
class Formats
|
||||||
attr_reader :config, :names, :scripts
|
attr_reader :config
|
||||||
|
|
||||||
def initialize(config)
|
def initialize(config)
|
||||||
self.config = config
|
self.config = config
|
||||||
@names = []
|
|
||||||
@scripts = {}
|
@scripts = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def freeze
|
def freeze
|
||||||
@names.freeze
|
|
||||||
@scripts.freeze
|
@scripts.freeze
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def register(name, **kwargs)
|
def register(name:, script:)
|
||||||
|
name = String(name).to_sym
|
||||||
|
script = String(script).to_sym
|
||||||
Referator.validate_name! name
|
Referator.validate_name! name
|
||||||
raise 'Already exists' if names.index(name) || scripts.key?(name)
|
Referator.validate_name! script
|
||||||
|
config.scripts.vars! script, script_vars.keys
|
||||||
|
raise 'Already exists' if @scripts.key? name
|
||||||
|
|
||||||
script = Script.new(**kwargs.merge(workdir: config.workdir,
|
@scripts[name] = script
|
||||||
vars: script_vars.keys))
|
|
||||||
|
|
||||||
names << name
|
|
||||||
scripts[name] = script
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def exists!(name)
|
def exists!(name)
|
||||||
scripts[name] or raise 'Unknown format'
|
Referator.validate_name! name
|
||||||
|
@scripts[name] or raise 'Unknown format'
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(template, name, notes)
|
def render(template, name, notes)
|
||||||
exists! name
|
exists! name
|
||||||
scripts[name].call(
|
config.scripts.run(
|
||||||
|
@scripts[name],
|
||||||
**script_vars(
|
**script_vars(
|
||||||
template:,
|
template:,
|
||||||
format: name,
|
format: name,
|
||||||
|
|
62
lib/referator/config/scripts.rb
Normal file
62
lib/referator/config/scripts.rb
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Referator
|
||||||
|
class Config
|
||||||
|
class Scripts
|
||||||
|
attr_reader :config
|
||||||
|
|
||||||
|
def initialize(config)
|
||||||
|
self.config = config
|
||||||
|
@scripts = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def freeze
|
||||||
|
@scripts.freeze
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def register(name:, workdir: nil, vars: [], **kwargs)
|
||||||
|
name = String(name).to_sym
|
||||||
|
Referator.validate_name! name
|
||||||
|
raise 'Already exists' if @scripts.key? name
|
||||||
|
|
||||||
|
script = Script.new(
|
||||||
|
**kwargs,
|
||||||
|
workdir: workdir || config.workdir,
|
||||||
|
vars: vars.map { |var| var.instance_of?(String) ? var.to_sym : var },
|
||||||
|
)
|
||||||
|
|
||||||
|
@scripts[name] = script
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def vars!(name, vars)
|
||||||
|
unless vars.instance_of? Array
|
||||||
|
raise TypeError, "Expected #{Array}, got #{vars.class}"
|
||||||
|
end
|
||||||
|
|
||||||
|
Referator.validate_name! name
|
||||||
|
vars.each { |var| Referator.validate_script_var! var }
|
||||||
|
|
||||||
|
script = @scripts[name] or raise 'Invalid script'
|
||||||
|
|
||||||
|
raise 'Invalid vars' unless script.vars.sort == vars.sort
|
||||||
|
end
|
||||||
|
|
||||||
|
def run(name, **vars)
|
||||||
|
vars! name, vars.keys
|
||||||
|
@scripts[name].call(**vars)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def config=(config)
|
||||||
|
unless config.instance_of? Config
|
||||||
|
raise TypeError, "Expected #{Config}, got #{config.class}"
|
||||||
|
end
|
||||||
|
|
||||||
|
@config = config
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
31
test.rb
31
test.rb
|
@ -87,6 +87,20 @@ def cmd(name, data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
###################
|
||||||
|
# REGISTER_SCRIPT #
|
||||||
|
###################
|
||||||
|
|
||||||
|
cmd :REGISTER_SCRIPT,
|
||||||
|
{ name: :render,
|
||||||
|
vars: %i[template format notes],
|
||||||
|
args: ['test/render.rb',
|
||||||
|
{ template: nil },
|
||||||
|
{ format: nil }],
|
||||||
|
stdin: ['{"notes":',
|
||||||
|
{ notes: nil },
|
||||||
|
'}'] }
|
||||||
|
|
||||||
#####################
|
#####################
|
||||||
# REGISTER_CATEGORY #
|
# REGISTER_CATEGORY #
|
||||||
#####################
|
#####################
|
||||||
|
@ -98,21 +112,8 @@ cmd :REGISTER_CATEGORY, :link
|
||||||
# REGISTER_FORMAT #
|
# REGISTER_FORMAT #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
cmd :REGISTER_FORMAT, { name: :html,
|
cmd :REGISTER_FORMAT, { name: :html, script: :render }
|
||||||
args: ['test/render.rb',
|
cmd :REGISTER_FORMAT, { name: :json, script: :render }
|
||||||
{ template: nil },
|
|
||||||
{ format: nil }],
|
|
||||||
stdin: ['{"notes":',
|
|
||||||
{ notes: nil },
|
|
||||||
'}'] }
|
|
||||||
|
|
||||||
cmd :REGISTER_FORMAT, { name: :json,
|
|
||||||
args: ['test/render.rb',
|
|
||||||
{ template: nil },
|
|
||||||
{ format: nil }],
|
|
||||||
stdin: ['{"notes":',
|
|
||||||
{ notes: nil },
|
|
||||||
'}'] }
|
|
||||||
|
|
||||||
################
|
################
|
||||||
# REGISTER_REF #
|
# REGISTER_REF #
|
||||||
|
|
Loading…
Reference in a new issue