quby-compiler 0.4.2 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0693e46b261fa2268bf3a406811a27d3782bf34c9c6cb6de4e968206f4d1ab43'
4
- data.tar.gz: 7fc5013a7ca5893db68d1276ececc4996d0f72e75737bc8c7a7912d622c3d873
3
+ metadata.gz: 10321dd1aa2034171ba876aeb77622bde63eac65938ce0a0bec9663119461396
4
+ data.tar.gz: b98a34612065d23a611ae5d0437463fe20557fbc0c238d50034e528676565284
5
5
  SHA512:
6
- metadata.gz: 743ea4cc3c41f908806d6937aed62dac028d9684246d61f951846fb1657582f8a300dcc3f32bc590630425a254a3ef69543805f67ad0f0ebd271cd1219440f55
7
- data.tar.gz: af49465555957283e67aa518bf0fdeed8250b3888f98f0d68b1e7a19d8158792718a958f91d4b9e99077319125d26eb6e69ee3a232280c4c0d98ba05a062e7cb
6
+ metadata.gz: a3544401b0db094144e7de1e0e172499a26a37111b73c01abd9ba027c33ac992c0c580e15b3fbd552bf196f70c1ed3dccab12ccb064dc68d3e3d80ff98740bc1
7
+ data.tar.gz: 0dd6d579368d571f57ea15dc20a5711ae8ba7e548049f8fd0abaaa43f15d1ca1454c1711f573e3d6b5143d198cbca8878f37399219a67c8330396c6fb57eb09e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,34 @@
1
+ # 0.4.6
2
+
3
+ * Added type validations for boolean attributes
4
+ * roqua.json
5
+ * add unit to questions
6
+ * add title_question_key to questions
7
+ * add parent_question_key and parent_option_key to questions
8
+ * add child_question_keys to question_options
9
+ * models
10
+ * Made title_question attribute on question instead of adding options.last.questions.
11
+ * Made v1 serializer create backward compatible json (for now)
12
+ * Made v1 serializer add the full title_question json to question.title_question.
13
+
14
+ # 0.4.5
15
+
16
+ * roqua.json: Don't add inner titles and placeholder options
17
+
18
+ # 0.4.4
19
+
20
+ * quby-compile will now validate, not compile invalid questionnaires and return 1 when any invalid.
21
+ * roqua.json
22
+ * add questions hash
23
+ * quby2.json
24
+ * add `as` to all questions, defaulting to type.
25
+ * add `minimum` and `maximum` to integer/float questions.
26
+ * add `step`, `defaultPosition`, `startThumbHidden`, `valueTooltip` and `labels` to slider questions.
27
+
28
+ # 0.4.3
29
+
30
+ * roqua.json: add scores hash.
31
+
1
32
  # 0.4.2
2
33
 
3
34
  * quby2: Added labels to slider questions.
data/exe/quby-compile CHANGED
@@ -30,11 +30,21 @@ end
30
30
 
31
31
  lookup_tables = Quby::Compiler::Entities::LookupTables.new(lookup_tables_path)
32
32
 
33
+ validation_errors_found = false
33
34
  paths.each do |path|
34
35
  puts "Compiling #{path}"
35
36
 
36
37
  key = File.basename(File.dirname(path))
37
38
  sourcecode = File.read(path)
39
+
40
+ definition = Quby::Compiler.validate(key, sourcecode, lookup_tables: lookup_tables)
41
+ if definition.errors.any?
42
+ error = definition.errors[:sourcecode].first
43
+ STDERR.puts error[:message], error[:backtrace], ''
44
+ validation_errors_found = true
45
+ next # let's not create new output files for questionnaires with errors
46
+ end
47
+
38
48
  compiled = Quby::Compiler.compile(key, sourcecode, path: path, lookup_tables: lookup_tables)
39
49
 
40
50
  FileUtils.mkdir_p(File.join(output_path, key))
@@ -45,3 +55,7 @@ paths.each do |path|
45
55
  end
46
56
  end
47
57
  end
58
+
59
+ if validation_errors_found
60
+ exit(1)
61
+ end
@@ -114,16 +114,6 @@ module Quby
114
114
  def initialize(key, **options, &block)
115
115
  super
116
116
  @default_question_options = options[:default_question_options] || {}
117
- @title_question = nil
118
- end
119
-
120
- def build
121
- if @title_question
122
- @question.options.last.questions << @title_question
123
- @title_question = nil
124
- end
125
-
126
- super
127
117
  end
128
118
 
129
119
  def title_question(key, **options, &block)
@@ -138,7 +128,7 @@ module Quby
138
128
  question = QuestionBuilder.build(key, **options, &block)
139
129
 
140
130
  @questionnaire.register_question(question)
141
- @title_question = question
131
+ @question.title_question = question
142
132
  end
143
133
 
144
134
  def question(key, **options, &block)
@@ -29,9 +29,11 @@ module Quby
29
29
 
30
30
  # To hide old questions
31
31
  attr_accessor :hidden
32
+ validates :hidden, inclusion: {in: [true, false, nil], message: "must be boolean"}
32
33
 
33
34
  # Whether to skip the uniqueness validation on radio and select option values
34
35
  attr_reader :allow_duplicate_option_values
36
+ validates :allow_duplicate_option_values, inclusion: {in: [true, false, nil], message: "must be boolean"}
35
37
 
36
38
  # In what modes do we display this question
37
39
  # NOTE We always display questions in print-view (if they have an answer)
@@ -40,9 +42,13 @@ module Quby
40
42
  # Multiple-choice questions have options to choose from
41
43
  attr_accessor :options
42
44
 
45
+ # string question that is displayed after the title of this question.
46
+ attr_accessor :title_question
47
+
43
48
  # Question validation fails when there are no title and no context_free_title.
44
49
  # When :allow_blank_titles => true passed, validation does not fail. Any other value will raise the failure.
45
50
  attr_accessor :allow_blank_titles
51
+ validates :allow_blank_titles, inclusion: {in: [true, false, nil], message: "must be boolean"}
46
52
 
47
53
  # Minimum and maximum values for float and integer types
48
54
  attr_accessor :minimum
@@ -72,6 +78,7 @@ module Quby
72
78
 
73
79
  # Whether this radio question is deselectable
74
80
  attr_accessor :deselectable
81
+ validates :deselectable, inclusion: {in: [true, false, nil], message: "must be boolean"}
75
82
 
76
83
  # Some questions are a tree.
77
84
  attr_accessor :parent
@@ -79,6 +86,7 @@ module Quby
79
86
 
80
87
  # Whether we can collapse this in bulk view
81
88
  attr_accessor :disallow_bulk
89
+ validates :disallow_bulk, inclusion: {in: [true, false, nil], message: "must be boolean"}
82
90
 
83
91
  # This question should not validate itself unless the depends_on question is answered.
84
92
  # May also be an array of "#{question_key}_#{option_key}" strings that specify options
@@ -115,6 +123,7 @@ module Quby
115
123
  attr_accessor :row_span
116
124
 
117
125
  attr_accessor :default_invisible
126
+ validates :default_invisible, inclusion: {in: [true, false], message: "must be boolean"}
118
127
 
119
128
  # Slider only: where to place the sliding thing by default
120
129
  # Can have value :hidden for a hidden handle.
@@ -240,14 +249,17 @@ module Quby
240
249
  parentOptionKey: parent_option_key,
241
250
  deselectable: deselectable,
242
251
  presentation: presentation,
243
- as: as,
252
+ as: as || type, # default to type so typescript can narrow on it.
244
253
  questionGroup: question_group
245
254
  ).tap do |json|
246
255
  json[:unit] = unit if %i[integer float string].include?(type) && as != :slider
247
- json[:labels] = labels if as == :slider
248
256
  end
249
257
  end
250
258
 
259
+ def title_question?
260
+ presentation == :next_to_title
261
+ end
262
+
251
263
  # Returns all keys belonging to html inputs generated by this question.
252
264
  def input_keys
253
265
  if options.blank?
@@ -4,6 +4,8 @@ module Quby
4
4
  module Compiler
5
5
  module Entities
6
6
  class QuestionOption
7
+ include ActiveModel::Validations
8
+
7
9
  MARKDOWN_ATTRIBUTES = %w(description).freeze
8
10
 
9
11
  attr_reader :key
@@ -11,16 +13,17 @@ module Quby
11
13
  attr_reader :description
12
14
  attr_reader :questions
13
15
  attr_reader :inner_title
16
+ validates :inner_title, inclusion: {in: [true, false, nil], message: "must be boolean"}
14
17
  attr_reader :hides_questions
15
18
  attr_reader :shows_questions
16
19
  attr_reader :hidden
20
+ validates :hidden, inclusion: {in: [true, false], message: "must be boolean"}
17
21
  attr_reader :placeholder
22
+ validates :placeholder, inclusion: {in: [true, false], message: "must be boolean"}
18
23
  attr_reader :question
19
24
  attr_reader :view_id
20
25
  attr_reader :input_key
21
26
 
22
- attr_reader :start_chosen
23
-
24
27
  def initialize(key, question, options = {})
25
28
  @key = key
26
29
  @question = question
@@ -0,0 +1,23 @@
1
+ module Quby::Compiler::Entities::Questions::Concerns
2
+ module Slider
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ validates :minimum, :maximum, presence: true, if: -> { as == :slider }
7
+ end
8
+
9
+ def as_json(options = {})
10
+ if as == :slider
11
+ super.merge(
12
+ step: step,
13
+ defaultPosition: default_position.is_a?(Numeric) ? default_position : minimum,
14
+ startThumbHidden: default_position == :hidden,
15
+ valueTooltip: input_data[:value_tooltip] || false,
16
+ labels: labels
17
+ )
18
+ else
19
+ super
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,22 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'concerns/slider'
4
+
3
5
  module Quby
4
6
  module Compiler
5
7
  module Entities
6
8
  module Questions
7
9
  class FloatQuestion < Question
10
+ include Concerns::Slider
11
+
8
12
  def as_json(options = {})
9
13
  super.merge(
10
14
  minimum: minimum,
11
- maximum: maximum,
12
- step: 0.01, # fixed in v1.
13
- # defaultPosition: default_position # Needs discussion, can be number or string "hidden"
15
+ maximum: maximum
14
16
  )
15
17
  end
16
18
 
17
19
  def size
18
20
  @size || 30
19
21
  end
22
+
23
+ def step
24
+ 0.01
25
+ end
20
26
  end
21
27
  end
22
28
  end
@@ -1,22 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'concerns/slider'
4
+
3
5
  module Quby
4
6
  module Compiler
5
7
  module Entities
6
8
  module Questions
7
9
  class IntegerQuestion < Question
10
+ include Concerns::Slider
11
+
8
12
  def as_json(options = {})
9
13
  super.merge(
10
14
  minimum: minimum,
11
- maximum: maximum,
12
- step: 1, # fixed in v1.
13
- # defaultPosition: default_position # Needs discussion, can be number or string "hidden"
15
+ maximum: maximum
14
16
  )
15
17
  end
16
18
 
17
19
  def size
18
20
  @size || 30
19
21
  end
22
+
23
+ def step
24
+ 1
25
+ end
20
26
  end
21
27
  end
22
28
  end
@@ -61,6 +61,7 @@ module Quby
61
61
  question_type: question.type,
62
62
  key: question.key,
63
63
  title: question.title,
64
+ title_question: (question_as_json(question.title_question) if question.title_question),
64
65
  context_free_title: question.context_free_title,
65
66
  description: question.description,
66
67
  presentation: question.presentation,
@@ -96,7 +97,7 @@ module Quby
96
97
  case question
97
98
  when Quby::Compiler::Entities::Questions::CheckboxQuestion
98
99
  base_options.merge(
99
- options: question.options.map { |option| option_as_json(option) },
100
+ options: options_as_json(question),
100
101
  check_all_option: question.check_all_option,
101
102
  uncheck_all_option: question.uncheck_all_option,
102
103
  maximum_checked_allowed: question.maximum_checked_allowed,
@@ -114,7 +115,7 @@ module Quby
114
115
  )
115
116
  when Quby::Compiler::Entities::Questions::DeprecatedQuestion
116
117
  base_options.merge(
117
- options: question.options.map { |option| option_as_json(option) }
118
+ options: options_as_json(question)
118
119
  )
119
120
  when Quby::Compiler::Entities::Questions::FloatQuestion
120
121
  base_options.merge(
@@ -130,11 +131,11 @@ module Quby
130
131
  )
131
132
  when Quby::Compiler::Entities::Questions::RadioQuestion
132
133
  base_options.merge(
133
- options: question.options.map { |option| option_as_json(option) }
134
+ options: options_as_json(question)
134
135
  )
135
136
  when Quby::Compiler::Entities::Questions::SelectQuestion
136
137
  base_options.merge(
137
- options: question.options.map { |option| option_as_json(option) }
138
+ options: options_as_json(question)
138
139
  )
139
140
  when Quby::Compiler::Entities::Questions::StringQuestion
140
141
  base_options.merge(
@@ -177,6 +178,16 @@ module Quby
177
178
  end
178
179
  end
179
180
 
181
+ # Also adds the title question under the last option.
182
+ # TODO old dsl put it there, remove after quby gem uses title_question
183
+ def options_as_json(question)
184
+ question.options.map { |option| option_as_json(option) }.tap do |options_json|
185
+ if question.title_question
186
+ options_json.last[:questions] << question_as_json(question.title_question)
187
+ end
188
+ end
189
+ end
190
+
180
191
  def option_as_json(option)
181
192
  {
182
193
  key: option.key,
@@ -2,6 +2,18 @@ module Quby
2
2
  module Compiler
3
3
  module Outputs
4
4
  class RoquaSerializer
5
+ QUBY_TYPE_TO_ROQUA_TYPE = {
6
+ check_box: 'multi_select',
7
+ date: 'date_parts',
8
+ float: 'float',
9
+ integer: 'integer',
10
+ radio: 'single_select',
11
+ scale: 'single_select',
12
+ select: 'single_select',
13
+ string: 'text',
14
+ textarea: 'text'
15
+ }
16
+
5
17
  attr_reader :questionnaire
6
18
 
7
19
  def initialize(questionnaire)
@@ -22,6 +34,8 @@ module Quby
22
34
  tags: questionnaire.tags.to_h.keys,
23
35
  charts: charts,
24
36
  outcome_tables_schema: outcome_tables_schema,
37
+ questions: questions,
38
+ scores: scores,
25
39
  }
26
40
  end
27
41
 
@@ -139,7 +153,112 @@ module Quby
139
153
  tables: tables,
140
154
  }
141
155
  end
156
+
157
+ # {v_1: {.., options: {..}, date_parts: {..}}, ..}
158
+ # nils are removed from hash.
159
+ # key [string]
160
+ # sbg_key [nil/string] _not_ defaulted to key
161
+ # type [string]
162
+ # title [nil/string] no html
163
+ # context_free_title [nil/string] no html, defaulted to title
164
+ # unit [nil/string] no html (not in quby either)
165
+ # deprecated [nil/true] only show if filled for old answers
166
+ # default_invisible [nil/true] only show if filled or when all questions are shown
167
+ # parent_question_key [nil/string] title_questions or question defined under option
168
+ # parent_option_key [nil/string] question defined under option
169
+ # title_question_key [nil/string]
170
+ # date_parts [nil/{..}]
171
+ # options: [nil/{..}]
172
+ def questions
173
+ questionnaire.questions.reject{_1.type == :hidden}.to_h { |question|
174
+ [
175
+ question.key,
176
+ {
177
+ key: question.key,
178
+ sbg_key: question.sbg_key,
179
+ type: QUBY_TYPE_TO_ROQUA_TYPE.fetch(question.type),
180
+ title: strip_tags_without_html_encode(question.title),
181
+ context_free_title: strip_tags_without_html_encode(question.context_free_title),
182
+ unit: question.unit,
183
+ deprecated: question.hidden.presence,
184
+ default_invisible: question.default_invisible.presence,
185
+ parent_question_key: question.parent&.key,
186
+ parent_option_key: question.parent_option_key,
187
+ title_question_key: question.title_question&.key,
188
+ date_parts: date_parts_for(question),
189
+ options: options_for(question)
190
+ }.compact
191
+ ]
192
+ }
193
+ end
194
+
195
+ # {year: {key: "v_date_yyyy"}, ..}
196
+ # key [string]
197
+ def date_parts_for(question)
198
+ return nil unless question.type == :date
199
+
200
+ question.components.to_h { |component|
201
+ [
202
+ component,
203
+ {
204
+ key: question.send("#{component}_key")
205
+ }
206
+ ]
207
+ }
208
+ end
209
+
210
+ # {a1: {..}}
211
+ # key [string]
212
+ # value [nil/string] nil for check_box, string otherwise
213
+ # description [nil/string] context_free_description, no html
214
+ # child_question_keys [nil/[string]]
215
+ def options_for(question)
216
+ return nil if question.options.empty?
217
+
218
+ question.options.reject { |option| option.inner_title || option.placeholder }
219
+ .to_h { |option|
220
+ [
221
+ option.key,
222
+ {
223
+ key: option.key,
224
+ value: (option.value.to_s unless question.type == :check_box),
225
+ description: strip_tags_without_html_encode(option.context_free_description),
226
+ child_question_keys: option.questions.map(&:key).presence
227
+ }.compact
228
+ ]
229
+ }
230
+ end
231
+
232
+ # [{ key: { .., subscores: { subkey: {..} } } }]
233
+ def scores
234
+ questionnaire.score_schemas.transform_values { |score|
235
+ {
236
+ key: score.key,
237
+ label: score.label,
238
+ subscores: subscores_for(score)
239
+ }
240
+ }
241
+ end
242
+
243
+ # { subkey: {..} }
244
+ def subscores_for(score)
245
+ score.subscore_schemas.to_h { |subscore|
246
+ [
247
+ subscore.key,
248
+ {
249
+ key: subscore.key,
250
+ label: subscore.label,
251
+ export_key: subscore.export_key,
252
+ only_for_export: subscore.only_for_export.presence
253
+ }.compact
254
+ ]
255
+ }
256
+ end
257
+
258
+ def strip_tags_without_html_encode(html)
259
+ Nokogiri::HTML(html).text.presence
260
+ end
142
261
  end
143
262
  end
144
263
  end
145
- end
264
+ end
@@ -100,6 +100,9 @@ module Quby
100
100
  def validate_question_options(questionnaire, question)
101
101
  question.options.each do |option|
102
102
  msg_base = "Question #{option.question.key} option #{option.key}"
103
+ unless option.valid?
104
+ fail "#{msg_base} #{option.errors.full_messages.to_sentence}"
105
+ end
103
106
  to_be_hidden_questions_exist_and_not_subquestion?(questionnaire, option, msg_base: msg_base)
104
107
  to_be_shown_questions_exist_and_not_subquestion?(questionnaire, option, msg_base: msg_base)
105
108
  end
@@ -1,5 +1,5 @@
1
1
  module Quby
2
2
  module Compiler
3
- VERSION = "0.4.2"
3
+ VERSION = "0.4.6"
4
4
  end
5
5
  end
data/lib/quby/compiler.rb CHANGED
@@ -38,7 +38,7 @@ module Quby
38
38
  )
39
39
  end
40
40
 
41
- def self.validate(key, sourcecode)
41
+ def self.validate(key, sourcecode, lookup_tables:)
42
42
  Quby::Compiler::Instance.new(lookup_tables: lookup_tables).validate(
43
43
  key: key,
44
44
  sourcecode: sourcecode,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quby-compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-15 00:00:00.000000000 Z
11
+ date: 2022-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -204,6 +204,7 @@ files:
204
204
  - lib/quby/compiler/entities/question_option.rb
205
205
  - lib/quby/compiler/entities/questionnaire.rb
206
206
  - lib/quby/compiler/entities/questions/checkbox_question.rb
207
+ - lib/quby/compiler/entities/questions/concerns/slider.rb
207
208
  - lib/quby/compiler/entities/questions/date_question.rb
208
209
  - lib/quby/compiler/entities/questions/deprecated_question.rb
209
210
  - lib/quby/compiler/entities/questions/float_question.rb