Class: Quby::Compiler::Outputs::QubyFrontendV1Serializer
- Inherits:
-
Object
- Object
- Quby::Compiler::Outputs::QubyFrontendV1Serializer
- Defined in:
- lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb
Instance Attribute Summary collapse
-
#questionnaire ⇒ Object
readonly
Returns the value of attribute questionnaire.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #expand_depends_on_input_keys(question) ⇒ Object
- #flags ⇒ Object
-
#initialize(questionnaire) ⇒ QubyFrontendV1Serializer
constructor
A new instance of QubyFrontendV1Serializer.
- #item_as_json(item) ⇒ Object
- #lookup_tables ⇒ Object
- #option_as_json(option) ⇒ Object
-
#options_as_json(question) ⇒ Object
Also adds the title question under the last option.
- #panels ⇒ Object
- #question_as_json(question) ⇒ Object
- #range_as_json(range) ⇒ Object
- #score_calculations ⇒ Object
- #score_schemas ⇒ Object
- #table_as_json(table) ⇒ Object
- #text_as_json(text) ⇒ Object
- #textvars ⇒ Object
- #validation_as_json(validation) ⇒ Object
Constructor Details
#initialize(questionnaire) ⇒ QubyFrontendV1Serializer
Returns a new instance of QubyFrontendV1Serializer.
9 10 11 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 9 def initialize(questionnaire) @questionnaire = questionnaire end |
Instance Attribute Details
#questionnaire ⇒ Object (readonly)
Returns the value of attribute questionnaire.
7 8 9 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 7 def questionnaire @questionnaire end |
Instance Method Details
#as_json(options = {}) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 269 def as_json( = {}) { key: questionnaire.key, title: questionnaire.title, abortable: questionnaire.abortable, enable_previous_questionnaire_button: questionnaire., default_answer_value: questionnaire.default_answer_value, leave_page_alert: questionnaire.leave_page_alert, allow_hotkeys: questionnaire.allow_hotkeys, language: questionnaire.language, extra_css: questionnaire.extra_css, panels: panels, score_calculations: score_calculations, score_schemas: score_schemas, flags: flags, textvars: textvars, lookup_tables: lookup_tables, } end |
#expand_depends_on_input_keys(question) ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 300 def (question) return unless question.depends_on.present? questionnaire.(question.depends_on) rescue => e raise e.class, "Question #{question.key} depends_on contains an error: #{e.}" end |
#flags ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 236 def flags questionnaire.flags.transform_values do |flag| { key: flag.key, description_true: flag.description_true, description_false: flag.description_false, description: flag.description, 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, } end end |
#item_as_json(item) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 23 def item_as_json(item) case item when Quby::Compiler::Entities::Text text_as_json(item) when Quby::Compiler::Entities::Question question_as_json(item) when Quby::Compiler::Entities::Table table_as_json(item) when Quby::Compiler::Entities::InfoBlock item.items.map { item_as_json(_1) } end end |
#lookup_tables ⇒ Object
264 265 266 267 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 264 def lookup_tables # TODO: Figure out something better. For now, no interest in walking a tree and converting Range objects to something JSON-able. YAML.dump(questionnaire.lookup_tables) end |
#option_as_json(option) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 193 def option_as_json(option) { key: (option.key unless option.inner_title?), value: option.value, description: option.label ? "#{option.label} #{option.description}" : option.description, context_free_description: option.context_free_description, questions: option.questions.map {|question| question_as_json(question)}, inner_title: option.inner_title, hides_questions: option.hides_questions, shows_questions: option.shows_questions, hidden: option.hidden, placeholder: option.placeholder, } end |
#options_as_json(question) ⇒ Object
Also adds the title question under the last option. TODO old dsl put it there, remove after quby gem uses title_question
185 186 187 188 189 190 191 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 185 def (question) question..map { |option| option_as_json(option) }.tap do || if question.title_question .last[:questions] << question_as_json(question.title_question) end end end |
#panels ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 13 def panels questionnaire.panels.map do |panel| { key: panel.key, title: panel.title, items: panel.items.reject { |item| item.respond_to?(:table) && item.table }.flat_map { |item| item_as_json(item) } } end end |
#question_as_json(question) ⇒ Object
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 154 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 60 def question_as_json(question) = { type: 'question', question_type: question.type, key: question.key, title: question.title, title_question: (question_as_json(question.title_question) if question.title_question), context_free_title: question.context_free_title, description: question.description, presentation: question.presentation, hidden: question.hidden, depends_on: (question), default_position: question.default_position, col_span: question.col_span, row_span: question.row_span, validations: question.validations.map {|validation| validation_as_json(validation) }, raw_content: question.raw_content, switch_cycle: question.switch_cycle, sbg_key: question.sbg_key, allow_duplicate_option_values: question.allow_duplicate_option_values, allow_blank_titles: question.allow_blank_titles, as: question.as, display_modes: question.display_modes, autocomplete: question.autocomplete, show_values: question.show_values, deselectable: question.deselectable, disallow_bulk: question.disallow_bulk, score_header: question.score_header, sets_textvar: question.sets_textvar, default_invisible: question.default_invisible, question_group: question.question_group, group_minimum_answered: question.group_minimum_answered, group_maximum_answered: question.group_maximum_answered, value_tooltip: question.input_data[:value_tooltip], parent_option_key: question.parent_option_key, } case question when Quby::Compiler::Entities::Questions::CheckboxQuestion .merge( options: (question), check_all_option: question.check_all_option, uncheck_all_option: question.uncheck_all_option, maximum_checked_allowed: question.maximum_checked_allowed, minimum_checked_required: question.minimum_checked_required ) when Quby::Compiler::Entities::Questions::DateQuestion .merge( components: question.components, required_components: question.required_components, year_key: question.year_key, month_key: question.month_key, day_key: question.day_key, hour_key: question.hour_key, minute_key: question.minute_key, ) when Quby::Compiler::Entities::Questions::DeprecatedQuestion .merge( options: (question) ) when Quby::Compiler::Entities::Questions::FloatQuestion .merge( labels: question.labels, unit: question.unit, size: question.size, ) when Quby::Compiler::Entities::Questions::IntegerQuestion .merge( labels: question.labels, unit: question.unit, size: question.size, ) when Quby::Compiler::Entities::Questions::RadioQuestion .merge( options: (question) ) when Quby::Compiler::Entities::Questions::SelectQuestion .merge( options: (question) ) when Quby::Compiler::Entities::Questions::StringQuestion .merge( unit: question.unit, size: question.size, ) when Quby::Compiler::Entities::Questions::TextQuestion .merge( lines: question.lines ) else raise "Unknown item type" end end |
#range_as_json(range) ⇒ Object
290 291 292 293 294 295 296 297 298 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 290 def range_as_json(range) return unless range { begin: range.begin, end: range.end, exclude_end: range.exclude_end? } end |
#score_calculations ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 208 def score_calculations questionnaire.score_calculations.transform_values do |score_calculation| { key: score_calculation.key, label: score_calculation.label, sbg_key: score_calculation.sbg_key, options: score_calculation., sourcecode: score_calculation.sourcecode } end end |
#score_schemas ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 220 def score_schemas questionnaire.score_schemas.transform_values do |schema| { key: schema.key, label: schema.label, subscore_schemas: schema.subscore_schemas.map do |subschema| { key: subschema.key, label: subschema.label, export_key: subschema.export_key, } end } end end |
#table_as_json(table) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 49 def table_as_json(table) { type: 'table', columns: table.columns, title: table.title, description: table.description, show_option_desc: table.show_option_desc, items: table.items.map { |item| item_as_json(item) } } end |
#text_as_json(text) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 36 def text_as_json(text) { type: 'text', str: text.str, html_content: text.html_content, display_in: text.display_in, col_span: text.col_span, row_span: text.row_span, raw_content: text.raw_content, switch_cycle: text.switch_cycle, } end |
#textvars ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 253 def textvars questionnaire.textvars.transform_values do |textvar| { key: textvar.key, description: textvar.description, default: textvar.default, depends_on_flag: textvar.depends_on_flag, } end end |
#validation_as_json(validation) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/quby/compiler/outputs/quby_frontend_v1_serializer.rb', line 156 def validation_as_json(validation) case validation[:type] when :requires_answer validation.slice(:type, :explanation) when :answer_group_minimum, :answer_group_maximum validation.slice(:type, :explanation, :group, :value) when :valid_integer, :valid_float validation.slice(:type, :explanation) when :valid_date validation.slice(:type, :explanation, :subtype).merge(value_type: validation[:value].class.to_s) when :minimum, :maximum validation.slice(:type, :explanation, :value, :subtype).merge(value_type: validation[:value].class.to_s) when :too_many_checked validation.slice(:type, :explanation, :uncheck_all_key) when :minimum_checked_required validation.slice(:type, :explanation, :minimum_checked_value) when :maximum_checked_allowed validation.slice(:type, :explanation, :maximum_checked_value) when :regexp validation.slice(:type, :explanation).merge(matcher: validation.fetch(:matcher).source) when :not_all_checked validation.slice(:type, :explanation, :check_all_key) else raise "Unknown validation type: #{validation.inspect}" end end |