Class: Quby::Compiler::Outputs::RoquaSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/compiler/outputs/roqua_serializer.rb

Constant Summary collapse

QUBY_TYPE_TO_ROQUA_TYPE =
{
  check_box: 'multi_select',
  date: 'date_parts',
  float: 'float',
  integer: 'integer',
  radio: 'single_select',
  scale: 'single_select',
  select: 'single_select',
  string: 'text',
  textarea: 'text'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire) ⇒ RoquaSerializer

Returns a new instance of RoquaSerializer.



19
20
21
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 19

def initialize(questionnaire)
  @questionnaire = questionnaire
end

Instance Attribute Details

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



17
18
19
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 17

def questionnaire
  @questionnaire
end

Instance Method Details

#as_json(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 23

def as_json(options = {})
  {
    key: questionnaire.key,
    versions: versions,
    keys: questionnaire.roqua_keys,
    roqua_keys: questionnaire.roqua_keys,
    sbg_key: questionnaire.sbg_key,
    sbg_domains: questionnaire.sbg_domains,
    outcome_regeneration_requested_at: questionnaire.outcome_regeneration_requested_at,
    deactivate_answers_requested_at: questionnaire.deactivate_answers_requested_at,
    respondent_types: questionnaire.respondent_types,
    tags: questionnaire.tags.to_h.keys,
    charts: charts,
    outcome_tables_schema: outcome_tables_schema,
    outcome_description: questionnaire.outcome_description,
    questions: questions,
    scores: scores,
    anonymous_conditions: questionnaire.anonymous_conditions,
    flags: flags,
    textvars: textvars,
    license: questionnaire.license,
    licensor: questionnaire.licensor,
  }
end

#chart_baseline(baseline) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 97

def chart_baseline(baseline)
  case baseline
  when nil
    nil
  when Float, Integer
    {default: {default: baseline}}
  when Hash
    baseline
  end
end

#chartsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 59

def charts
  {
    overview: questionnaire.charts.overview && {
      subscore: questionnaire.charts.overview.subscore,
      y_max: questionnaire.charts.overview.y_max,
    },
    others: questionnaire.charts.map do |chart|
      case chart
      when Quby::Compiler::Entities::Charting::LineChart
        {
          tonality: chart.tonality,
          baseline: chart_baseline(chart.baseline),
          clinically_relevant_change: chart.clinically_relevant_change,
        }
      when Quby::Compiler::Entities::Charting::OverviewChart
        {
          subscore: chart.subscore,
          y_max: chart.y_max,
        }
      else
        {}
      end.merge(
        key: chart.key,
        type: chart.type,
        title: chart.title,
        plottables: chart.plottables,
        y_categories: chart.y_categories,
        y_range_categories: chart.y_range_categories,
        chart_type: chart.chart_type,
        y_range: chart.y_range,
        tick_interval: chart.tick_interval,
        plotbands: chart.plotbands,
        plotlines: chart.plotlines
      )
    end
  }
end

#date_parts_for(question) ⇒ Object

{key: “v_date_yyyy”, ..} key [string]



214
215
216
217
218
219
220
221
222
223
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 214

def date_parts_for(question)
  return nil unless question.type == :date

  question.components.map { |component|
    {
      part: component,
      key: question.send("#{component}_key")
    }
  }
end

#flagsObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 269

def flags
  questionnaire.flags.map do |key, flag|
    {
      key: flag.key,
      description: flag.description,
      description_true: flag.description_true,
      description_false: flag.description_false,
      internal: flag.internal,
      trigger_on: flag.trigger_on,
      shows_questions: flag.shows_questions,
      hides_questions: flag.hides_questions,
      depends_on: flag.depends_on,
      default: flag.default,
      default_in_interface: flag.default # TODO: Remove this when Roqua supports default natively
    }
  end
end

#options_for(question) ⇒ Object

{..} key [string] value [nil/string] nil for check_box, string otherwise description [nil/string] context_free_description or parsed/stripped description, no html child_question_keys [nil/]



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 230

def options_for(question)
  return nil if question.options.empty?

  question.options.reject { |option| option.inner_title || option.placeholder }
                  .map { |option|
    {
      key: option.key,
      value: (option.value.to_s unless question.type == :check_box),
      description: option.context_free_description || parse_markdown_and_strip_tags(option.description),
      child_question_keys: option.questions.map(&:key).presence,
      unchecks_all_others: question.try(:uncheck_all_option) && question.uncheck_all_option == option.key
    }.compact
  }
end

#outcome_tables_from_definitionObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 124

def outcome_tables_from_definition
  # hash of tables, with the score keys (rows) and subscore keys (columns) used for each
  tables = []
  # hash of `subscore_key: subscore_label` pairs used in tables
  headers = {}

  questionnaire.outcome_tables.each do |table|
    tables << {
      key: table.key,
      name: table.name,
      default_collapsed: table.default_collapsed,
      score_keys: table.score_keys,
      subscore_keys: table.subscore_keys
    }.compact

    table.subscore_keys.each do |subscore_key|
      table.score_keys.find do |score_key|
        subschema = questionnaire.score_schemas[score_key].subscore_schemas.find do |subschema|
          subschema.key == subscore_key
        end
        headers[subscore_key] = subschema&.label
      end
    end
  end

  {
    headers: headers,
    tables: tables,
  }
end

#outcome_tables_from_score_schemasObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 155

def outcome_tables_from_score_schemas
  # hash of tables, with the score keys (rows) and subscore keys (columns) used for each
  tables = Hash.new{ |hash, key| hash[key] = {score_keys: Set.new, subscore_keys: Set.new } }
  # hash of `subscore_key: subscore_label` pairs used in tables
  headers = {}

  questionnaire.score_schemas.values.each do |schema|
    schema.subscore_schemas.each do |subschema|
      next if subschema.outcome_table.blank?
      tables[subschema.outcome_table][:key] = subschema.outcome_table
      tables[subschema.outcome_table][:subscore_keys] << subschema.key
      tables[subschema.outcome_table][:score_keys] << schema.key
      headers[subschema.key] = subschema.label
    end
  end

  {
    headers: headers,
    tables: tables.values,
  }
end

#outcome_tables_schemaObject

configuration for outcome tables. tables:

<outcome_table_name:Symbol>: # each entry is a table.
  score_keys: Set[<schema.key:Symbol>] # rows in the table
  subscore_keys: Set[<subschema.key:Symbol>] # columns  in the table

headers:

<subscore_key:Symbol>: <subscore.label:String> # headers for each subscore key for all tables.


116
117
118
119
120
121
122
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 116

def outcome_tables_schema
  if questionnaire.outcome_tables.present?
    outcome_tables_from_definition
  else
    outcome_tables_from_score_schemas
  end
end

#parse_markdown_and_strip_tags(markdown) ⇒ Object



298
299
300
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 298

def parse_markdown_and_strip_tags(markdown)
  strip_tags_without_html_encode(Quby::Compiler::MarkdownParser.new(markdown).to_html)
end

#questionsObject

{.., options: {.., date_parts: ..}, ..} nils are removed from hash. key [string] sbg_key [nil/string] not defaulted to key type [string] title [nil/string] no html context_free_title [nil/string] no html, not defaulted to title! unit [nil/string] no html (not in quby either) deprecated [nil/true] only show if filled for old answers default_invisible [nil/true] only show if filled or when all questions are shown parent_question_key [nil/string] title_questions or question defined under option parent_option_key [nil/string] question defined under option title_question_key [nil/string] date_parts [nil/..] options: [nil/..]



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 192

def questions
  questionnaire.sorted_questions.reject{_1.type == :hidden}.map { |question|
    {
      key: question.key,
      sbg_key: question.sbg_key,
      type: QUBY_TYPE_TO_ROQUA_TYPE.fetch(question.type),
      title: parse_markdown_and_strip_tags(question.title),
      context_free_title: question.context_free_title,
      unit: question.unit,
      deprecated: question.hidden.presence,
      default_invisible: question.default_invisible.presence,
      parent_question_key: question.parent&.key,
      parent_option_key: question.parent_option_key,
      title_question_key: question.title_question&.key,
      date_parts: date_parts_for(question),
      options: options_for(question)
    }.compact
  }
end

#scoresObject

{ key: { .., subscores: { subkey: .. } } }


246
247
248
249
250
251
252
253
254
255
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 246

def scores
  questionnaire.score_schemas.values.map { |score|
    {
      key: score.key,
      label: score.label,
      sbg_key: score.sbg_key,
      subscores: subscores_for(score)
    }.compact
  }
end

#strip_tags_without_html_encode(html) ⇒ Object



302
303
304
305
306
307
308
309
310
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 302

def strip_tags_without_html_encode(html)
  Nokogiri::HTML(html).tap do |doc|
    doc.css('br, hr, img').each { |node| node.replace(' ') }
  end \
    .text
    .gsub(/\s+/, ' ')
    .strip
    .presence
end

#subscores_for(score) ⇒ Object

{ subkey: .. }



258
259
260
261
262
263
264
265
266
267
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 258

def subscores_for(score)
  score.subscore_schemas.map { |subscore|
    {
      key: subscore.key,
      label: subscore.label,
      export_key: subscore.export_key,
      type: subscore.type
    }.compact
  }
end

#textvarsObject



287
288
289
290
291
292
293
294
295
296
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 287

def textvars
  questionnaire.textvars.map do |key, textvar|
    {
      key: textvar.key,
      description: textvar.description,
      default: textvar.default,
      depends_on_flag: textvar.depends_on_flag
    }
  end
end

#versionsObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 48

def versions
  questionnaire.versions.map do |version|
    {
      number: version.number,
      release_notes: version.release_notes,
      regenerate_outcome: version.regenerate_outcome,
      deactivate_answers: version.deactivate_answers
    }
  end
end