Module: Quby::Questionnaires::Deserializer
- Defined in:
- lib/quby/questionnaires/deserializer.rb
Class Method Summary collapse
- .build_flag(attrs) ⇒ Object
- .build_option(questionnaire, question, option_json) ⇒ Object
- .build_question(questionnaire, item_json, parent: nil, table: nil) ⇒ Object
- .build_question_validation(attrs) ⇒ Object
- .build_score_calculation(attrs) ⇒ Object
- .build_score_schema(attributes) ⇒ Object
- .build_text(item_json) ⇒ Object
- .build_textvar(attrs) ⇒ Object
- .deserialize_range(range_attributes) ⇒ Object
-
.from_json(json) ⇒ Object
This symbolizes various things.
- .load_item(questionnaire, item_json, panel: nil) ⇒ Object
- .load_panel(questionnaire, panel_json) ⇒ Object
Class Method Details
.build_flag(attrs) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/quby/questionnaires/deserializer.rb', line 303 def self.build_flag(attrs) Entities::Flag.new( key: attrs.fetch("key").to_sym, description_true: attrs.fetch("description_true"), description_false: attrs.fetch("description_false"), description: attrs.fetch("description"), internal: attrs.fetch("internal"), trigger_on: attrs.fetch("trigger_on"), shows_questions: attrs.fetch("shows_questions").map(&:to_sym), hides_questions: attrs.fetch("hides_questions").map(&:to_sym), depends_on: attrs.fetch("depends_on"), # TODO: emperically determined to be a string in DSL, is that right? default: attrs.fetch("default") { attrs.fetch("default_in_interface") } ) end |
.build_option(questionnaire, question, option_json) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/quby/questionnaires/deserializer.rb', line 213 def self.build_option(questionnaire, question, option_json) option = Entities::QuestionOption.new(option_json.fetch("key")&.to_sym, question, value: option_json.fetch("value"), description: option_json.fetch("description"), context_free_description: option_json.fetch("context_free_description"), inner_title: option_json.fetch("inner_title"), hides_questions: option_json.fetch("hides_questions").map(&:to_sym), shows_questions: option_json.fetch("shows_questions").map(&:to_sym), hidden: option_json.fetch("hidden"), placeholder: option_json.fetch("placeholder"), ) option_json.fetch("questions").each do |question_json| subquestion = build_question(questionnaire, question_json, parent: question) questionnaire.register_question(subquestion) option.questions << subquestion end option end |
.build_question(questionnaire, item_json, parent: nil, table: nil) ⇒ Object
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 155 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/quby/questionnaires/deserializer.rb', line 106 def self.build_question(questionnaire, item_json, parent: nil, table: nil) key = item_json.fetch("key").to_sym attributes = { questionnaire: questionnaire, parent: parent, type: item_json.fetch("question_type").to_sym, title: item_json.fetch("title"), context_free_title: item_json.fetch("context_free_title"), description: item_json.fetch("description"), presentation: item_json.fetch("presentation").to_sym, hidden: item_json.fetch("hidden"), depends_on: item_json.fetch("depends_on")&.map(&:to_sym), default_position: item_json.fetch("default_position"), validations: item_json.fetch("validations").map {|attrs| build_question_validation(attrs)}, table: table, col_span: item_json.fetch("col_span"), row_span: item_json.fetch("row_span"), raw_content: item_json.fetch("raw_content", nil), # only selectable via options passed in DSL, not via DSL methods # many apply only to certain types of questions allow_duplicate_option_values: item_json.fetch("allow_duplicate_option_values"), allow_blank_titles: item_json.fetch("allow_blank_titles"), as: item_json.fetch("as")&.to_sym, display_modes: item_json.fetch("display_modes")&.map(&:to_sym), autocomplete: item_json.fetch("autocomplete"), show_values: item_json.fetch("show_values").to_sym, deselectable: item_json.fetch("deselectable"), disallow_bulk: item_json.fetch("disallow_bulk"), score_header: item_json.fetch("score_header").to_sym, sets_textvar: item_json.fetch("sets_textvar"), default_invisible: item_json.fetch("default_invisible"), question_group: item_json.fetch("question_group"), # sometimes string, sometimes a symbol in the DSL. Just have to hope this works group_minimum_answered: item_json.fetch("group_minimum_answered"), group_maximum_answered: item_json.fetch("group_maximum_answered"), value_tooltip: item_json.fetch("value_tooltip"), # might be able to deduce from tree structure parent_option_key: item_json.fetch("parent_option_key")&.to_sym } case item_json.fetch("question_type") when "check_box" Entities::Questions::CheckboxQuestion.new(key, attributes.merge( check_all_option: item_json.fetch("check_all_option")&.to_sym, uncheck_all_option: item_json.fetch("uncheck_all_option")&.to_sym, maximum_checked_allowed: item_json.fetch("maximum_checked_allowed"), minimum_checked_required: item_json.fetch("minimum_checked_required"), )).tap do |question| item_json.fetch("options").each do |option_json| question. << build_option(questionnaire, question, option_json) end end when "date" Entities::Questions::DateQuestion.new(key, attributes.merge( components: item_json.fetch("components").map(&:to_sym), required_components: item_json.fetch("required_components").map(&:to_sym), year_key: item_json.fetch("year_key")&.to_sym, month_key: item_json.fetch("month_key")&.to_sym, day_key: item_json.fetch("day_key")&.to_sym, hour_key: item_json.fetch("hour_key")&.to_sym, minute_key: item_json.fetch("minute_key")&.to_sym, )) when "deprecated", "hidden" Entities::Questions::DeprecatedQuestion.new(key, attributes).tap do |question| item_json.fetch("options").each do |option_json| question. << build_option(questionnaire, question, option_json) end end when "float" Entities::Questions::FloatQuestion.new(key, attributes.merge( labels: item_json.fetch("labels"), unit: item_json.fetch("unit"), size: item_json.fetch("size"), )) when "integer" Entities::Questions::IntegerQuestion.new(key, attributes.merge( labels: item_json.fetch("labels"), unit: item_json.fetch("unit"), size: item_json.fetch("size"), )) when "radio", "scale" Entities::Questions::RadioQuestion.new(key, attributes).tap do |question| item_json.fetch("options").each do |option_json| question. << build_option(questionnaire, question, option_json) end end when "select" Entities::Questions::SelectQuestion.new(key, attributes).tap do |question| item_json.fetch("options").each do |option_json| question. << build_option(questionnaire, question, option_json) end end when "string" Entities::Questions::StringQuestion.new(key, attributes.merge( unit: item_json.fetch("unit"), size: item_json.fetch("size"), )) when "textarea" Entities::Questions::TextQuestion.new(key, attributes.merge( lines: item_json.fetch("lines"), )) else raise "Unknown question type: #{item_json}" end end |
.build_question_validation(attrs) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/quby/questionnaires/deserializer.rb', line 234 def self.build_question_validation(attrs) base_validation = { type: attrs.fetch("type").to_sym, explanation: attrs["explanation"] # not always specified for min/max validation } case attrs.fetch("type") when "requires_answer" base_validation when "answer_group_minimum", "answer_group_maximum" base_validation.merge( group: attrs.fetch("group"), # TODO: sometimes a symbol, sometimes a string in the original, but I hope it doesn't matter value: attrs.fetch("value") ) when "valid_integer", "valid_float" base_validation when "valid_date" base_validation.merge( subtype: attrs.fetch("subtype").to_sym ) when "minimum", "maximum" value = case attrs.fetch("value_type") when "Date" Date.parse(attrs.fetch("value")) when "DateTime" DateTime.parse(attrs.fetch("value")) when "Time", "ActiveSuport::TimeWithZone" Time.zone.parse(attrs.fetch("value")) else attrs.fetch("value") end base_validation.merge( value: value, subtype: attrs.fetch("subtype").to_sym, ) when "too_many_checked" base_validation.merge( uncheck_all_key: attrs.fetch("uncheck_all_key").to_sym ) when "minimum_checked_required" base_validation.merge( minimum_checked_value: attrs.fetch("minimum_checked_value") ) when "maximum_checked_allowed" base_validation.merge( maximum_checked_value: attrs.fetch("maximum_checked_value") ) when "regexp" base_validation.merge( matcher: Regexp.new(attrs.fetch("matcher")) ) when "not_all_checked" base_validation.merge( check_all_key: attrs.fetch("check_all_key").to_sym ) else raise "Unknown validation type: #{attrs.inspect}" end end |
.build_score_calculation(attrs) ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/quby/questionnaires/deserializer.rb', line 295 def self.build_score_calculation(attrs) Entities::ScoreCalculation.new(attrs.fetch("key").to_sym, label: attrs.fetch("label"), options: attrs.fetch("options").symbolize_keys, sourcecode: attrs.fetch("sourcecode"), ) end |
.build_score_schema(attributes) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/quby/questionnaires/deserializer.rb', line 327 def self.build_score_schema(attributes) Entities::ScoreSchema.new( key: attributes.fetch("key").to_sym, label: attributes.fetch("label"), subscore_schemas: attributes.fetch("subscore_schemas").map do |subschema| { key: subschema.fetch("key").to_sym, label: subschema.fetch("label"), export_key: subschema.fetch("export_key").to_sym, } end ) end |
.build_text(item_json) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/quby/questionnaires/deserializer.rb', line 95 def self.build_text(item_json) Entities::Text.new(item_json.fetch("str"), { html_content: item_json.fetch("html_content"), display_in: item_json.fetch("display_in").map(&:to_sym), col_span: item_json.fetch("col_span"), row_span: item_json.fetch("row_span"), raw_content: item_json.fetch("raw_content"), switch_cycle: item_json.fetch("switch_cycle") }) end |
.build_textvar(attrs) ⇒ Object
318 319 320 321 322 323 324 325 |
# File 'lib/quby/questionnaires/deserializer.rb', line 318 def self.build_textvar(attrs) Entities::Textvar.new( key: attrs.fetch("key").to_sym, description: attrs.fetch("description"), default: attrs.fetch("default"), depends_on_flag: attrs.fetch("depends_on_flag")&.to_sym ) end |
.deserialize_range(range_attributes) ⇒ Object
341 342 343 344 |
# File 'lib/quby/questionnaires/deserializer.rb', line 341 def self.deserialize_range(range_attributes) return unless range_attributes Range.new(range_attributes.fetch("begin"), range_attributes.fetch("end"), range_attributes.fetch("exclude_end")) end |
.from_json(json) ⇒ Object
This symbolizes various things. Do not run on arbitrary JSON.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/quby/questionnaires/deserializer.rb', line 7 def self.from_json(json) # TODO: last_update Entities::Questionnaire.new(json.fetch("key"), json).tap do |questionnaire| questionnaire.title = json.fetch("title") questionnaire.abortable = json.fetch("abortable") questionnaire. = json.fetch("enable_previous_questionnaire_button") questionnaire.default_answer_value = json.fetch("default_answer_value") questionnaire.leave_page_alert = json.fetch("leave_page_alert") questionnaire.allow_hotkeys = json.fetch("allow_hotkeys") questionnaire.language = json.fetch("language").try(:to_sym) questionnaire.extra_css = json.fetch("extra_css") questionnaire.flags = json.fetch("flags").with_indifferent_access.transform_values do |attrs| build_flag(attrs) end questionnaire.textvars = json.fetch("textvars").with_indifferent_access.transform_values do |attrs| build_textvar(attrs) end questionnaire.lookup_tables = YAML.unsafe_load(json.fetch("lookup_tables")).transform_values do |attrs| Quby::TableBackend::RangeTree.new(levels: attrs[:levels], tree: attrs[:tree]) end questionnaire.score_calculations = json.fetch("score_calculations").with_indifferent_access.transform_values do |attrs| build_score_calculation(attrs) end questionnaire.score_schemas = json.fetch("score_schemas").with_indifferent_access.transform_values do |schema| build_score_schema(schema) end json.fetch("panels").each do |panel_json| load_panel(questionnaire, panel_json) end end end |
.load_item(questionnaire, item_json, panel: nil) ⇒ 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 |
# File 'lib/quby/questionnaires/deserializer.rb', line 60 def self.load_item(questionnaire, item_json, panel: nil) case item_json.fetch("type") when "text" panel.items << build_text(item_json) when "question" question = build_question(questionnaire, item_json) questionnaire.register_question(question) panel.items << question when "table" table = Entities::Table.new( title: item_json.fetch("title"), description: item_json.fetch("description"), columns: item_json.fetch("columns"), show_option_desc: item_json.fetch("show_option_desc"), ) panel.items << table item_json.fetch("items").each do |table_item_json| case table_item_json.fetch("type") when "text" table.items << build_text(table_item_json) when "question" question = build_question(questionnaire, table_item_json, table: table) questionnaire.register_question(question) table.items << question panel.items << question else raise "Unknown table item: #{table_item_json}" end end else raise "Unknown item: #{item_json}" end end |
.load_panel(questionnaire, panel_json) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/quby/questionnaires/deserializer.rb', line 45 def self.load_panel(questionnaire, panel_json) panel = Entities::Panel.new( questionnaire: questionnaire, key: panel_json.fetch("key"), title: panel_json.fetch("title"), items: [] ) panel_json.fetch("items").each do |item_json| load_item(questionnaire, item_json, panel: panel) end questionnaire.add_panel(panel) end |