Class: Quby::Compiler::Entities::VisibilityRule
- Inherits:
-
Object
- Object
- Quby::Compiler::Entities::VisibilityRule
- Defined in:
- lib/quby/compiler/entities/visibility_rule.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
Class Method Summary collapse
- .for_condition(condition, show_questions: [], hide_questions: []) ⇒ Object
- .from(question) ⇒ Object
- .from_flag(flag) ⇒ Object
- .rules_for_option(question, option, type:) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(condition:, action:) ⇒ VisibilityRule
constructor
A new instance of VisibilityRule.
Constructor Details
#initialize(condition:, action:) ⇒ VisibilityRule
Returns a new instance of VisibilityRule.
88 89 90 91 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 88 def initialize(condition:, action:) @condition = condition @action = action end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
86 87 88 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 86 def action @action end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
86 87 88 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 86 def condition @condition end |
Class Method Details
.for_condition(condition, show_questions: [], hide_questions: []) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 63 def self.for_condition(condition, show_questions: [], hide_questions: []) [ *show_questions.map { |show_key| VisibilityRule.new( condition:, action: { type: 'show_question', field_key: show_key } ) }, *hide_questions.map { |hide_key| VisibilityRule.new( condition:, action: { type: 'hide_question', field_key: hide_key } ) } ] end |
.from(question) ⇒ Object
5 6 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 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 5 def self.from(question) [].tap do |rules| rules.concat question.visibility_rules # Transform "default invisible" into just being hidden by itself, # since any other question showing it will take precedence anyway. if question.default_invisible rules << new(condition: { type: 'always', field_key: question.key }, action: { type: 'hide_question', field_key: question.key }) end if question.hidden # hide by default, show if data is already present. rules << new(condition: { type: 'always', field_key: question.key }, action: { type: 'hide_question', field_key: question.key }) rules << new(condition: { type: 'answered', evenWhenHidden: true, field_key: question.key }, action: { type: 'show_question', field_key: question.key }) end case question.type when :radio, :scale, :select question..each do |option| rules.concat rules_for_option(question, option, type: 'equal') end when :check_box question..each do |option| rules.concat rules_for_option(question, option, type: 'contains') end end end end |
.from_flag(flag) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 49 def self.from_flag(flag) condition = { type: "flag_equal", flag_key: flag.key, trigger_on: flag.trigger_on } [].tap do |rules| flag.shows_questions.each do |question_key| rules << VisibilityRule.new(condition: condition, action: { type: "show_question", field_key: question_key }) end flag.hides_questions.each do |question_key| rules << VisibilityRule.new(condition: condition, action: { type: "hide_question", field_key: question_key }) end end end |
.rules_for_option(question, option, type:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 35 def self.rules_for_option(question, option, type:) [].tap do |rules| option.shows_questions.each do |shows_question| rules << new(condition: { type: type, field_key: question.key, value: option.key }, action: { type: 'show_question', field_key: shows_question }) end option.hides_questions.each do |hides_question| rules << new(condition: { type: type, field_key: question.key, value: option.key }, action: { type: 'hide_question', field_key: hides_question }) end end end |
Instance Method Details
#as_json ⇒ Object
93 94 95 96 97 98 |
# File 'lib/quby/compiler/entities/visibility_rule.rb', line 93 def as_json { condition: condition.transform_keys { |key| key.to_s.camelize(:lower) }, action: action.transform_keys { |key| key.to_s.camelize(:lower) }, } end |