refactor internals, get rid of YAML anchors while still allowing to be lazy
This commit is contained in:
parent
c826d9d2c8
commit
4f7d74606c
|
@ -1,86 +1,73 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'yaml'
|
||||
require "yaml"
|
||||
|
||||
# Generates some questions.
|
||||
module QuestionGenerator
|
||||
# Version of QuestionGenerator
|
||||
VERSION = "1.0.0"
|
||||
VERSION = "1.1.0"
|
||||
|
||||
class << self
|
||||
# The base path to the questions (e.g. +'/home/nilsding/questions'+).
|
||||
attr_accessor :question_base_path
|
||||
attr_reader :question_base_path
|
||||
# The default locale, as a symbol.
|
||||
attr_accessor :default_locale
|
||||
end
|
||||
|
||||
module_function
|
||||
|
||||
# Generates a new question.
|
||||
# @param options [Hash] A customizable set of options.
|
||||
# @option options [Symbol] :locale (@default_locale) The target locale
|
||||
# @option options [String] :prefix Prefix of the question, e.g. +'¿'+
|
||||
# @option options [String] :suffix ('?') Suffix of the question, e.g. +' ?'+
|
||||
# @option options [Boolean] :use_compiled (true) Use compiled questions
|
||||
# instead of generating it. See also {compile}
|
||||
# @param :locale [Symbol] The target locale
|
||||
# @param :prefix [String] Prefix of the question, e.g. +'¿'+
|
||||
# @param :suffix [String] Suffix of the question, e.g. +' ?'+
|
||||
# @return [String] String containing the generated question.
|
||||
def self.generate(options = {})
|
||||
opts = {
|
||||
locale: @default_locale,
|
||||
prefix: '',
|
||||
suffix: '?',
|
||||
use_compiled: true
|
||||
}.merge!(options)
|
||||
if opts[:use_compiled] and !@compiled[opts[:locale]].nil?
|
||||
opts[:prefix] + @compiled[opts[:locale]].sample + opts[:suffix]
|
||||
else
|
||||
questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
|
||||
opts[:prefix] + get_question(questions).strip + opts[:suffix]
|
||||
end
|
||||
def generate(locale: @default_locale, prefix: "", suffix: "?")
|
||||
compile(locale:) unless @compiled.key?(locale)
|
||||
|
||||
prefix + @compiled[locale].sample + suffix
|
||||
end
|
||||
|
||||
# Compiles all the questions and stores it into the +@compiled+ hash.
|
||||
# @param options [Hash] A customizable set of options.
|
||||
# @option options [Symbol] :locale (@default_locale) The target locale
|
||||
def self.compile(options = {})
|
||||
opts = {
|
||||
locale: @default_locale
|
||||
}.merge!(options)
|
||||
questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
|
||||
@compiled[@default_locale] = build(questions)
|
||||
# @param :locale [Symbol] The target locale
|
||||
def compile(locale: @default_locale)
|
||||
questions = YAML.load_file(File.expand_path("#{locale}.yml", @question_base_path))
|
||||
@compiled[locale] = build(questions)
|
||||
end
|
||||
|
||||
def question_base_path=(path)
|
||||
raise Errno::ENOENT.new(path) unless Dir.exist?(path)
|
||||
|
||||
@compiled = {} # new dir, force a recompile
|
||||
@question_base_path = path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.get_question questions
|
||||
question = ""
|
||||
if questions.is_a? Hash
|
||||
key = questions.keys.sample
|
||||
value = questions[key]
|
||||
question = "#{key} #{get_question(value)}"
|
||||
elsif questions.is_a? Array
|
||||
question = get_question questions.sample
|
||||
elsif questions.is_a? String
|
||||
question = questions
|
||||
end
|
||||
question
|
||||
end
|
||||
module_function
|
||||
|
||||
def self.build(questions, q = "")
|
||||
def build(questions, q = "")
|
||||
ary = []
|
||||
if questions.is_a? Hash
|
||||
|
||||
case questions
|
||||
when Hash
|
||||
questions.each do |k, v|
|
||||
ary << build(v, "#{q}#{k} ")
|
||||
Array(k).each do |variant|
|
||||
ary << build(v, "#{q}#{variant} ")
|
||||
end
|
||||
elsif questions.is_a? Array
|
||||
end
|
||||
when Array
|
||||
questions.each do |v|
|
||||
ary << build(v, q)
|
||||
end
|
||||
elsif questions.is_a? String
|
||||
when String
|
||||
return "#{q}#{questions}".strip
|
||||
end
|
||||
|
||||
ary.flatten
|
||||
end
|
||||
|
||||
@question_base_path = File.expand_path("../questions/", __FILE__)
|
||||
@question_base_path = File.expand_path("./questions/", __dir__)
|
||||
@default_locale = :en
|
||||
@compiled = {}
|
||||
end
|
||||
|
|
|
@ -47,17 +47,14 @@ Do:
|
|||
- muffins
|
||||
- video games
|
||||
know:
|
||||
too:
|
||||
much:
|
||||
about: &friends_know_much
|
||||
[much, too much]:
|
||||
about:
|
||||
- your:
|
||||
- life
|
||||
- hobbies
|
||||
- family
|
||||
- friends
|
||||
- you
|
||||
much:
|
||||
about: *friends_know_much
|
||||
What:
|
||||
- was:
|
||||
the:
|
||||
|
@ -118,11 +115,10 @@ What:
|
|||
- foxes
|
||||
- dogs
|
||||
- lizards
|
||||
- activity: &activity
|
||||
- [activity, activities]:
|
||||
do:
|
||||
you:
|
||||
- enjoy the most
|
||||
- activities: *activity
|
||||
- kind:
|
||||
of:
|
||||
animals:
|
||||
|
@ -133,7 +129,7 @@ What:
|
|||
really:
|
||||
find:
|
||||
cute:
|
||||
- in a person?
|
||||
- in a person
|
||||
- is:
|
||||
your:
|
||||
- first memory
|
||||
|
@ -169,7 +165,7 @@ What:
|
|||
- Facebook
|
||||
- YouTube
|
||||
the:
|
||||
best: &is_the_best
|
||||
[best, worst]:
|
||||
thing:
|
||||
- about:
|
||||
the:
|
||||
|
@ -188,7 +184,6 @@ What:
|
|||
fast:
|
||||
food:
|
||||
- chain
|
||||
worst: *is_the_best
|
||||
one:
|
||||
- thing you would like to become better at
|
||||
- was:
|
||||
|
@ -199,10 +194,9 @@ What:
|
|||
- did
|
||||
- ate
|
||||
- looked for
|
||||
best: &was_the_best
|
||||
[best, worst]:
|
||||
thing:
|
||||
- "you've eaten so far"
|
||||
worst: *was_the_best
|
||||
- languages do you know
|
||||
- apps do you use daily
|
||||
- 'is heavier: a kilogram of steel, or a kilogram of feathers'
|
||||
|
@ -219,6 +213,7 @@ Can:
|
|||
- piano
|
||||
- guitar
|
||||
- trumpet
|
||||
- saxophone
|
||||
- baseball
|
||||
- ski
|
||||
- cook
|
||||
|
|
Loading…
Reference in New Issue