quby-compiler 0.6.7 → 0.6.8

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: 3b15f3bb6d801cc4285f64d05d715cbf86326ae4a6aae32767b3e1bd720c805f
4
- data.tar.gz: 6ff6dbd94acf1c14ae61f6bc3eb96f6e462b4728ad2e781752cc8980380377ff
3
+ metadata.gz: 01cfba3183d5a4279b2c73b3c9cee99b676bb878fe4fb41750966dff9f3cee05
4
+ data.tar.gz: b73b3f4426dc2a4f995fd03164a49ef65d27a723d7de870fdeceaebf26efecc0
5
5
  SHA512:
6
- metadata.gz: 6fd74f49d869607c6306a0d6c6fe62088930cbfd7a894be53f04a16efa7f15846fb2876f37171f92df3137e180bec112e76f322d7a78c973474f2c16f7e45f88
7
- data.tar.gz: 15a0708156bcfb9563bc1c684da99a4a9ad523a3d6ff92571a4a3d1bacd8b1106b05ae629f1b76d60bcf178cee6cd54a1d7ae89f31d950165400cd7814597a7c
6
+ metadata.gz: 7babf0bc0256ed62390f442b34104eaf64778ad04a563fa3ffd239091bcca67f0d3058a42a67df4b8db50a12a8e6bd064636f290cc519233065c7d0a18175e9f
7
+ data.tar.gz: c65d1f03ed2219c8b3704a843323ef92bb019baef95889ee86d40ecc67fb4f4eda5a2bdb5905adf652178657b783232c85353db0fb726076efbaad1ea342ce21
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.6.8
2
+
3
+ * dsl_quby2.json: Add count_selected to s-expressions.
4
+ * dsl_quby2.json: Add cond, true and false to s-expressions. cond evaluates the body of the first clause whose test is true; each body must have the same type.
5
+ * dsl_quby2.json: single_select_label variables (also via cond) must be declared `type: :quest_i18n_key` and are serialized with that type.
6
+
1
7
  # 0.6.7
2
8
 
3
9
  * dsl_quby2.json: Add single_select_label to s-expressions.
@@ -23,6 +23,10 @@ module Quby::Compiler
23
23
  Entities::SexpVariables::SingleSelectLabel.new(op: :single_select_label, key: key)
24
24
  end
25
25
 
26
+ def count_selected(key)
27
+ Entities::SexpVariables::CountSelected.new(key: key)
28
+ end
29
+
26
30
  def number_value(key)
27
31
  Entities::SexpVariables::NumberValue.new(op: :number_value, key: key)
28
32
  end
@@ -53,6 +57,16 @@ module Quby::Compiler
53
57
  Entities::SexpVariables::NumberMethod.new(op: :round, value: value)
54
58
  end
55
59
 
60
+ # cond(clause(gt(left: number_value(:v_1), right: 2), 4), clause(true, 0))
61
+ # evaluates the body of the first clause whose test is true, so `clause(true, ...)` acts as the default case.
62
+ def cond(*clauses)
63
+ Entities::SexpVariables::Cond.new(op: :cond, clauses:)
64
+ end
65
+
66
+ def clause(test, body)
67
+ Entities::SexpVariables::Cond::Clause.new(test: wrap_scalars(test), body: wrap_scalars(body))
68
+ end
69
+
56
70
  def build
57
71
  Entities::SexpVariable.new(key:, calculation:, type:)
58
72
  end
@@ -67,9 +81,15 @@ module Quby::Compiler
67
81
  case value
68
82
  when Numeric
69
83
  Entities::SexpVariables::Number.new(op: :number, value: value)
84
+ when true
85
+ Entities::SexpVariables::True.new
86
+ when false
87
+ Entities::SexpVariables::False.new
88
+ when String
89
+ fail ArgumentError, "string literals are not supported in sexp_variables (they can't be translated), use string_value(:key) to reference a string question."
70
90
  else
71
91
  value
72
- end
92
+ end
73
93
  end
74
94
  end
75
95
  end
@@ -15,6 +15,10 @@ module Quby::Compiler::Entities
15
15
  validate_node(calculation, questionnaire:, type:)
16
16
  end
17
17
 
18
+ def as_json(options = nil)
19
+ {key:, type: node_type(calculation), calculation:}.as_json(options)
20
+ end
21
+
18
22
  private
19
23
 
20
24
  def validate_node(node, questionnaire:, type:)
@@ -30,6 +34,9 @@ module Quby::Compiler::Entities
30
34
  when SexpVariables::SingleSelectLabel
31
35
  validate_question_exist(questionnaire, node)
32
36
  validate_value_is_single_select(questionnaire, node)
37
+ when SexpVariables::CountSelected
38
+ validate_question_exist(questionnaire, node)
39
+ validate_value_is_multi_select(questionnaire, node)
33
40
  when SexpVariables::NumberReducer
34
41
  node.values.each do |value|
35
42
  validate_node(value, questionnaire:, type: :number)
@@ -41,13 +48,17 @@ module Quby::Compiler::Entities
41
48
  when SexpVariables::Comparer
42
49
  validate_node(node.left, questionnaire:, type: :number)
43
50
  validate_node(node.right, questionnaire:, type: :number)
51
+ when SexpVariables::Cond
52
+ node.clauses.each do |clause|
53
+ validate_node(clause.test, questionnaire:, type: :boolean)
54
+
55
+ validate_node(clause.body, questionnaire:, type: type)
56
+ end
44
57
  end
45
58
  end
46
59
 
47
60
  def validate_type(node, type:)
48
- return if type == :number && SexpVariables::NumericType === node
49
- return if type == :boolean && SexpVariables::BooleanType === node
50
- return if type == :string && SexpVariables::StringType === node
61
+ return if type == node_type(node)
51
62
 
52
63
  fail "sexp_variable #{key} has invalid type #{node.class} for expected type #{type}."
53
64
  end
@@ -78,6 +89,13 @@ module Quby::Compiler::Entities
78
89
  fail "sexp_variable #{key} uses non-single-select question #{sexp.key} for single_select_label."
79
90
  end
80
91
 
92
+ def validate_value_is_multi_select(questionnaire, sexp)
93
+ question = questionnaire.question_hash[sexp.key]
94
+ return if Questions::CheckboxQuestion === question
95
+
96
+ fail "sexp_variable #{key} uses non-multi-select question #{sexp.key} for count_selected."
97
+ end
98
+
81
99
  def validate_value_is_string(questionnaire, sexp)
82
100
  question = questionnaire.question_hash[sexp.key]
83
101
  case question
@@ -87,5 +105,20 @@ module Quby::Compiler::Entities
87
105
 
88
106
  fail "sexp_variable #{key} uses non-string question #{sexp.key} for string_value."
89
107
  end
108
+
109
+ def node_type(node)
110
+ case(node)
111
+ when SexpVariables::Cond # match first, since it's in all types.
112
+ node_type(node.clauses.first.body)
113
+ when SexpVariables::BooleanType
114
+ :boolean
115
+ when SexpVariables::NumericType
116
+ :number
117
+ when SexpVariables::StringType
118
+ :string
119
+ when SexpVariables::QuestI18nKeyType
120
+ :quest_i18n_key
121
+ end
122
+ end
90
123
  end
91
124
  end
@@ -4,36 +4,62 @@ module Quby::Compiler::Entities
4
4
  end
5
5
 
6
6
  # recursive dependencies, we so we define the classes first.
7
+ class Cond < Base; end
8
+
7
9
  class NumberValue < Base; end
8
10
  class Number < Base; end
9
11
  class NumberReducer < Base; end
10
12
  class NumberMethod < Base; end
13
+ class CountSelected < Base; end
14
+
11
15
  class BooleanReducer < Base; end
12
16
  class Comparer < Base; end
17
+ class True < Base; end
18
+ class False < Base; end
19
+
20
+ class StringValue < Base; end
21
+ class SingleSelectLabel < Base; end
13
22
 
14
- NumericType = Number | NumberReducer | NumberMethod | NumberValue
15
- BooleanType = BooleanReducer | Comparer
23
+ NumericType = Number | NumberReducer | NumberMethod | NumberValue | CountSelected | Cond
24
+ BooleanType = BooleanReducer | Comparer | True | False | Cond
25
+ StringType = StringValue | Cond
26
+ QuestI18nKeyType = SingleSelectLabel | Cond
27
+ AllTypes = NumericType | BooleanType | StringType | QuestI18nKeyType
16
28
 
17
29
  class NumberValue
18
30
  attribute :op, Quby::Types::Symbol.enum(:number_value)
19
31
  attribute :key, Quby::Types::Symbol
20
32
  end
21
33
 
22
- class StringValue < Base
34
+ class StringValue
23
35
  attribute :op, Quby::Types::Symbol.enum(:string_value)
24
36
  attribute :key, Quby::Types::Symbol
25
37
  end
26
38
 
27
39
  # label of the selected option of a single-select (radio, scale, select) question.
28
- class SingleSelectLabel < Base
40
+ class SingleSelectLabel
29
41
  attribute :op, Quby::Types::Symbol.enum(:single_select_label)
30
42
  attribute :key, Quby::Types::Symbol
31
43
  end
32
44
 
33
- StringType = StringValue | SingleSelectLabel
45
+ # returning a Boolean: always true. Used as the default case of a cond.
46
+ class True
47
+ attribute :op, Quby::Types::Symbol.default(:true).enum(:true)
48
+ end
49
+
50
+ # returning a Boolean: always false.
51
+ class False
52
+ attribute :op, Quby::Types::Symbol.default(:false).enum(:false)
53
+ end
54
+
55
+ # number of selected options of a multi-select (check_box) question.
56
+ class CountSelected
57
+ attribute :op, Quby::Types::Symbol.default(:count_selected).enum(:count_selected)
58
+ attribute :key, Quby::Types::Symbol
59
+ end
34
60
 
35
61
  class Number
36
- attribute :op, Quby::Types::Symbol.enum(:number)
62
+ attribute :op, Quby::Types::Symbol.enum(:number)
37
63
  attribute :value, Quby::Types::Integer | Quby::Types::Float
38
64
  end
39
65
 
@@ -58,8 +84,20 @@ module Quby::Compiler::Entities
58
84
 
59
85
  # returning a Number
60
86
  class NumberMethod
61
- attribute :op, Quby::Types::Symbol.enum(:round)
87
+ attribute :op, Quby::Types::Symbol.enum(:round)
62
88
  attribute :value, NumericType
63
89
  end
90
+
91
+ # evaluates the body of the first clause whose test is true.
92
+ # Each body should have the same type, which determines the result type.
93
+ class Cond
94
+ class Clause < Base
95
+ attribute :test, BooleanType
96
+ attribute :body, AllTypes
97
+ end
98
+
99
+ attribute :op, Quby::Types::Symbol.enum(:cond)
100
+ attribute :clauses, Quby::Types::Array.of(Clause).constrained(min_size: 1)
101
+ end
64
102
  end
65
103
  end
@@ -1,5 +1,5 @@
1
1
  module Quby
2
2
  module Compiler
3
- VERSION = "0.6.7"
3
+ VERSION = "0.6.8"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quby-compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis