questiongenerator 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1e2d199193a1473caaace799faf1d0128123694e57fc4f6960dcfe440781901
4
- data.tar.gz: 2adad2960babd375248131b4aedf5caabf1d6187e09e0110355980acd479f9b1
3
+ metadata.gz: 758784d5d1a0314b051f925492230ceabd04b25b5c3ffd5449949008940e7f14
4
+ data.tar.gz: fea85ed6d03dc237f32fa53cee8c6644fcd7a02bf173216845f1120c571785de
5
5
  SHA512:
6
- metadata.gz: a92ff8f5e93261b7d0b47b638fa4e790ca4ebd94106272f88fe11ac9e2c7374ebe617dd8d2b6d20f55554205232952bd42b38c32de0d068c1b0f2af85ddeea8d
7
- data.tar.gz: ce7c8ae44c2008a149e7f00b6f246314310b1676e91fe5f7d043a542da858c9417990c5c677da163472b54c0895738c57dc3c87050b917d4a9363525de1878bb
6
+ metadata.gz: c82dbab5148bfd888c3783ea1c5c4c37dadc6dcfec27ef5f6899fb8944cc23fbd432d41ebdaed8f4fdb25a5626f072d31a9730fa338ccb5ac8db05eac03137de
7
+ data.tar.gz: f03721ccb6170cd76dbd11663492972849b3e452f2c0ee0fec797c9fd29bb846f38a5c761a3eecfa1044973bdc2c2091b3146a87249db69a2cfe36b02355db7a
@@ -1,86 +1,73 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
3
+ require "yaml"
4
4
 
5
5
  # Generates some questions.
6
6
  module QuestionGenerator
7
7
  # Version of QuestionGenerator
8
- VERSION = "1.0.0"
8
+ VERSION = "1.1.0"
9
9
 
10
10
  class << self
11
11
  # The base path to the questions (e.g. +'/home/nilsding/questions'+).
12
- attr_accessor :question_base_path
12
+ attr_reader :question_base_path
13
13
  # The default locale, as a symbol.
14
14
  attr_accessor :default_locale
15
15
  end
16
16
 
17
+ module_function
18
+
17
19
  # Generates a new question.
18
20
  # @param options [Hash] A customizable set of options.
19
- # @option options [Symbol] :locale (@default_locale) The target locale
20
- # @option options [String] :prefix Prefix of the question, e.g. +'¿'+
21
- # @option options [String] :suffix ('?') Suffix of the question, e.g. +' ?'+
22
- # @option options [Boolean] :use_compiled (true) Use compiled questions
23
- # instead of generating it. See also {compile}
21
+ # @param :locale [Symbol] The target locale
22
+ # @param :prefix [String] Prefix of the question, e.g. +'¿'+
23
+ # @param :suffix [String] Suffix of the question, e.g. +' ?'+
24
24
  # @return [String] String containing the generated question.
25
- def self.generate(options = {})
26
- opts = {
27
- locale: @default_locale,
28
- prefix: '',
29
- suffix: '?',
30
- use_compiled: true
31
- }.merge!(options)
32
- if opts[:use_compiled] and !@compiled[opts[:locale]].nil?
33
- opts[:prefix] + @compiled[opts[:locale]].sample + opts[:suffix]
34
- else
35
- questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
36
- opts[:prefix] + get_question(questions).strip + opts[:suffix]
37
- end
25
+ def generate(locale: @default_locale, prefix: "", suffix: "?")
26
+ compile(locale:) unless @compiled.key?(locale)
27
+
28
+ prefix + @compiled[locale].sample + suffix
38
29
  end
39
30
 
40
31
  # Compiles all the questions and stores it into the +@compiled+ hash.
41
- # @param options [Hash] A customizable set of options.
42
- # @option options [Symbol] :locale (@default_locale) The target locale
43
- def self.compile(options = {})
44
- opts = {
45
- locale: @default_locale
46
- }.merge!(options)
47
- questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
48
- @compiled[@default_locale] = build(questions)
32
+ # @param :locale [Symbol] The target locale
33
+ def compile(locale: @default_locale)
34
+ questions = YAML.load_file(File.expand_path("#{locale}.yml", @question_base_path))
35
+ @compiled[locale] = build(questions)
36
+ end
37
+
38
+ def question_base_path=(path)
39
+ raise Errno::ENOENT.new(path) unless Dir.exist?(path)
40
+
41
+ @compiled = {} # new dir, force a recompile
42
+ @question_base_path = path
49
43
  end
50
44
 
51
45
  private
52
46
 
53
- def self.get_question questions
54
- question = ""
55
- if questions.is_a? Hash
56
- key = questions.keys.sample
57
- value = questions[key]
58
- question = "#{key} #{get_question(value)}"
59
- elsif questions.is_a? Array
60
- question = get_question questions.sample
61
- elsif questions.is_a? String
62
- question = questions
63
- end
64
- question
65
- end
66
-
67
- def self.build(questions, q = "")
68
- ary = []
69
- if questions.is_a? Hash
70
- questions.each do |k, v|
71
- ary << build(v, "#{q}#{k} ")
72
- end
73
- elsif questions.is_a? Array
74
- questions.each do |v|
75
- ary << build(v, q)
47
+ module_function
48
+
49
+ def build(questions, q = "")
50
+ ary = []
51
+
52
+ case questions
53
+ when Hash
54
+ questions.each do |k, v|
55
+ Array(k).each do |variant|
56
+ ary << build(v, "#{q}#{variant} ")
76
57
  end
77
- elsif questions.is_a? String
78
- return "#{q}#{questions}".strip
79
58
  end
80
- ary.flatten
59
+ when Array
60
+ questions.each do |v|
61
+ ary << build(v, q)
62
+ end
63
+ when String
64
+ return "#{q}#{questions}".strip
81
65
  end
82
66
 
83
- @question_base_path = File.expand_path("../questions/", __FILE__)
84
- @default_locale = :en
85
- @compiled = {}
67
+ ary.flatten
68
+ end
69
+
70
+ @question_base_path = File.expand_path("./questions/", __dir__)
71
+ @default_locale = :en
72
+ @compiled = {}
86
73
  end
data/lib/questions/en.yml CHANGED
@@ -47,17 +47,14 @@ Do:
47
47
  - muffins
48
48
  - video games
49
49
  know:
50
- too:
51
- much:
52
- about: &friends_know_much
50
+ [much, too much]:
51
+ about:
53
52
  - your:
54
53
  - life
55
54
  - hobbies
56
55
  - family
57
56
  - friends
58
57
  - you
59
- much:
60
- about: *friends_know_much
61
58
  What:
62
59
  - was:
63
60
  the:
@@ -118,11 +115,10 @@ What:
118
115
  - foxes
119
116
  - dogs
120
117
  - lizards
121
- - activity: &activity
118
+ - [activity, activities]:
122
119
  do:
123
120
  you:
124
121
  - enjoy the most
125
- - activities: *activity
126
122
  - kind:
127
123
  of:
128
124
  animals:
@@ -133,7 +129,7 @@ What:
133
129
  really:
134
130
  find:
135
131
  cute:
136
- - in a person?
132
+ - in a person
137
133
  - is:
138
134
  your:
139
135
  - first memory
@@ -169,7 +165,7 @@ What:
169
165
  - Facebook
170
166
  - YouTube
171
167
  the:
172
- best: &is_the_best
168
+ [best, worst]:
173
169
  thing:
174
170
  - about:
175
171
  the:
@@ -188,7 +184,6 @@ What:
188
184
  fast:
189
185
  food:
190
186
  - chain
191
- worst: *is_the_best
192
187
  one:
193
188
  - thing you would like to become better at
194
189
  - was:
@@ -199,10 +194,9 @@ What:
199
194
  - did
200
195
  - ate
201
196
  - looked for
202
- best: &was_the_best
197
+ [best, worst]:
203
198
  thing:
204
199
  - "you've eaten so far"
205
- worst: *was_the_best
206
200
  - languages do you know
207
201
  - apps do you use daily
208
202
  - 'is heavier: a kilogram of steel, or a kilogram of feathers'
@@ -219,6 +213,7 @@ Can:
219
213
  - piano
220
214
  - guitar
221
215
  - trumpet
216
+ - saxophone
222
217
  - baseball
223
218
  - ski
224
219
  - cook
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: questiongenerator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nilsding
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-26 00:00:00.000000000 Z
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,7 +83,7 @@ homepage: https://github.com/retrospring/questiongenerator
83
83
  licenses:
84
84
  - MIT
85
85
  metadata: {}
86
- post_install_message:
86
+ post_install_message:
87
87
  rdoc_options: []
88
88
  require_paths:
89
89
  - lib
@@ -98,8 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubygems_version: 3.1.6
102
- signing_key:
101
+ rubygems_version: 3.4.3
102
+ signing_key:
103
103
  specification_version: 4
104
104
  summary: A simple question generator.
105
105
  test_files: