From b83e340dbe0ecc175379b62413cda792d6b341e4 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 1 Oct 2023 00:11:32 +0400 Subject: [PATCH] Validate script vars --- lib/referator.rb | 14 ++++++++++++++ lib/referator/script.rb | 4 +--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/referator.rb b/lib/referator.rb index de83bba..846611f 100644 --- a/lib/referator.rb +++ b/lib/referator.rb @@ -18,6 +18,10 @@ module Referator NAME_RE = /\A\w+(_\w+)*\z/ SLUG_RE = /\A\w+(-\w+)*\z/ + SCRIPT_ENUMS = %i[format template].sort.freeze + SCRIPT_OBJS = %i[notes].sort.freeze + SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze + def self.validate_name!(name) name.freeze unless name.instance_of? Symbol @@ -37,4 +41,14 @@ module Referator slug end + + def self.validate_script_var!(var) + var.freeze + unless var.instance_of? Symbol + raise TypeError, "Expected #{String}, got #{slug.class}" + end + raise 'Invalid var' unless SCRIPT_VARS.include? var + + var + end end diff --git a/lib/referator/script.rb b/lib/referator/script.rb index 9e05096..747be7b 100644 --- a/lib/referator/script.rb +++ b/lib/referator/script.rb @@ -105,9 +105,7 @@ module Referator def vars=(vars) @vars = [*vars].uniq.sort.freeze.each do |var| - unless var.instance_of? Symbol - raise TypeError, "Expected #{Symbol}, got #{var.class}" - end + Referator.validate_script_var! var end end