quby-compiler 0.5.33 → 0.5.34

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: 4c1af309fb9a9b4b95c7ec4e7d41cc9e1f69a21bfbf2b7267649f50b7d87bffe
4
- data.tar.gz: 06caabaaed4da6c9951f37491e0d8e31817ec2998b3662bb09186d024a52ce52
3
+ metadata.gz: b2ea33225ad03f296485cdec6a219a1febb04b8dfb59a1f590c161a520d3c1ba
4
+ data.tar.gz: 3a9371ab504dcaf1435d972928f6a03139135ac540b9b6591677f1b6a1177b1a
5
5
  SHA512:
6
- metadata.gz: b46018e140022030f7aa573e02d9ac213fd8acd28c3d31b24ac4f06d079deade64f60a39455476e337ff34499eb6921449d4f73024d579eb61fe9aabfba0008c
7
- data.tar.gz: 821f576e5a389a6cd6f988e667e6440e3aecdc025443caf56a572773065d7142110f298331140b7555fe35867e11ebcfc26a16ab74a4656f4e28413b7a7d2aab
6
+ metadata.gz: 0feb9af9ef47f2a5789da4f2ee839eaee5d55e3cdea29712519ef9beb7328ae091d5cdc67d449ee49cec0330fc7dbca4ded34f225805b4682a45d5509819cf77
7
+ data.tar.gz: 16ba9e37aebb7e7ffb665902bb3b40525c2e281097de2795f708c0f794e4c6c861becc8d81428ce26d29266a30f2d57678cb62be37fcfe1eddea277c631f5f88
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.5.34
2
+
3
+ * Sorting visibility rules by question shown/hidden
4
+ * Added sexp_visibility_rule (quby2 only)
5
+
1
6
  # 0.5.33
2
7
 
3
8
  * quby_proxy: fixes for optgroups.
@@ -296,6 +296,16 @@ module Quby
296
296
  @questionnaire.check_checkbox_options_start_with_question_key = false
297
297
  end
298
298
 
299
+ def sexp_visibility_rule(sexp_key:, show_questions: [], hide_questions: [])
300
+ condition = {
301
+ type: 'sexp_variable_true',
302
+ sexp_key:
303
+ }
304
+ @questionnaire.sexp_visibility_rules.concat(
305
+ Entities::VisibilityRule.for_condition(condition, show_questions:, hide_questions:)
306
+ )
307
+ end
308
+
299
309
  private
300
310
 
301
311
  def default_panel_options
@@ -92,24 +92,9 @@ module Quby
92
92
  op:,
93
93
  value:
94
94
  }
95
- show_questions.each do |show_key|
96
- @question.visibility_rules << Entities::VisibilityRule.new(
97
- condition:,
98
- action: {
99
- type: 'show_question',
100
- field_key: show_key
101
- }
102
- )
103
- end
104
- hide_questions.each do |hide_key|
105
- @question.visibility_rules << Entities::VisibilityRule.new(
106
- condition:,
107
- action: {
108
- type: 'hide_question',
109
- field_key: hide_key
110
- }
111
- )
112
- end
95
+ @question.visibility_rules.concat(
96
+ Entities::VisibilityRule.for_condition(condition, show_questions:, hide_questions:)
97
+ )
113
98
  end
114
99
  end
115
100
 
@@ -55,6 +55,7 @@ module Quby
55
55
  @check_score_keys_consistency = true
56
56
  @lookup_tables = {}
57
57
  @sexp_variables = {}
58
+ @sexp_visibility_rules = []
58
59
  @versions = []
59
60
  @seeds_patch = {}
60
61
  @anonymous_conditions = Entities::AnonymousConditions.new
@@ -108,6 +109,7 @@ module Quby
108
109
  attr_accessor :outcome_tables
109
110
  attr_accessor :score_schemas
110
111
  attr_accessor :sexp_variables
112
+ attr_reader :sexp_visibility_rules
111
113
  attr_accessor :lookup_tables
112
114
  attr_accessor :anonymous_conditions
113
115
 
@@ -392,13 +394,18 @@ module Quby
392
394
  end
393
395
 
394
396
  # Order is important
395
- # quby2 follows them one by one and ignores rules by hidden question, so needs to be in order of interface.
396
- # Flags don't have this issue, so as long as they are first, their order doesn't matter.
397
+ # We sort by order of the question being shown/hidden with showing winning from hiding, since showing wins from hiding.
398
+ # This is important since a shown question can't be hidden, so each question is only ever changed once while looping over the rules.
397
399
  def visibility_rules
400
+ return @visibility_rules if @visibility_rules
401
+ question_order = sorted_questions.map.with_index{ [_1, _2] }.to_h
398
402
  @visibility_rules ||= [
399
403
  *flags.values.flat_map { |flag| VisibilityRule.from_flag(flag) },
400
- *sorted_questions.flat_map { |question| VisibilityRule.from(question) }
401
- ]
404
+ *sorted_questions.flat_map { |question| VisibilityRule.from(question) },
405
+ *sexp_visibility_rules
406
+ ].sort_by.with_index do |rule, idx|
407
+ [question_order[rule.action[:field_key]], rule.action[:type] == 'show_question' ? 0 : 1, idx]
408
+ end
402
409
  end
403
410
 
404
411
  private
@@ -60,6 +60,29 @@ module Quby
60
60
  end
61
61
  end
62
62
 
63
+ def self.for_condition(condition, show_questions: [], hide_questions: [])
64
+ [
65
+ *show_questions.map { |show_key|
66
+ VisibilityRule.new(
67
+ condition:,
68
+ action: {
69
+ type: 'show_question',
70
+ field_key: show_key
71
+ }
72
+ )
73
+ },
74
+ *hide_questions.map { |hide_key|
75
+ VisibilityRule.new(
76
+ condition:,
77
+ action: {
78
+ type: 'hide_question',
79
+ field_key: hide_key
80
+ }
81
+ )
82
+ }
83
+ ]
84
+ end
85
+
63
86
  attr_reader :condition, :action
64
87
 
65
88
  def initialize(condition:, action:)
@@ -29,6 +29,7 @@ module Quby
29
29
  validate_sexp_variables(questionnaire)
30
30
  validate_info_blocks(questionnaire)
31
31
  validate_checkbox_options(questionnaire)
32
+ validate_visiblity_rules(questionnaire)
32
33
  # Some compilation errors are Exceptions (pure syntax errors) and some StandardErrors (NameErrors)
33
34
  rescue Exception => exception # rubocop:disable Lint/RescueException
34
35
  definition.errors.add(:sourcecode, message: "Questionnaire error: #{definition.key}\n" \
@@ -417,6 +418,24 @@ scores_schema tables to the resulting seed."
417
418
  fail "uncheck_all_option key :#{question.uncheck_all_option} does not exist on question :#{question.key}"
418
419
  end
419
420
  end
421
+
422
+ def validate_visiblity_rules(questionnaire)
423
+ questionnaire.visibility_rules.each do |rule|
424
+ case rule.condition[:type]
425
+ when 'sexp_variable_true'
426
+ sexp_key = rule.condition[:sexp_key]
427
+ unless questionnaire.sexp_variables.key?(sexp_key)
428
+ fail "sexp_visibility_rule references nonexistent sexp variable :#{sexp_key}."
429
+ end
430
+ unless questionnaire.sexp_variables[sexp_key].type == :boolean
431
+ fail "sexp_visibility_rule references sexp variable :#{sexp_key} with non-boolean type"
432
+ end
433
+ end
434
+ unless questionnaire.question_hash.key?(rule.action[:field_key])
435
+ fail "visibility_rule references nonexistent question :#{rule.action[:field_key]}."
436
+ end
437
+ end
438
+ end
420
439
  end
421
440
  end
422
441
  end
@@ -1,5 +1,5 @@
1
1
  module Quby
2
2
  module Compiler
3
- VERSION = "0.5.33"
3
+ VERSION = "0.5.34"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quby-compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.33
4
+ version: 0.5.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-09-17 00:00:00.000000000 Z
10
+ date: 2025-09-23 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activemodel