Class: Inquirex::Rules::Contains
- Defined in:
- lib/inquirex/rules/contains.rb
Overview
Rule: the answer for the given field (as an array) includes the given value. Used for multi_enum steps (e.g. "Business in income_types").
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.from_h(hash) ⇒ Contains
Deserializes a Contains rule from a plain Hash.
Instance Method Summary collapse
-
#evaluate(answers) ⇒ Boolean
True when the field's answer, coerced to an Array, includes the value.
-
#initialize(field, value) ⇒ Contains
constructor
A new instance of Contains.
-
#to_h ⇒ Hash{String => Object}
Wire format, same shape .from_h accepts.
-
#to_s ⇒ String
Human-readable form, e.g.
Constructor Details
#initialize(field, value) ⇒ Contains
Returns a new instance of Contains.
12 13 14 15 16 17 |
# File 'lib/inquirex/rules/contains.rb', line 12 def initialize(field, value) super() @field = field.to_sym @value = value freeze end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
8 9 10 |
# File 'lib/inquirex/rules/contains.rb', line 8 def field @field end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/inquirex/rules/contains.rb', line 8 def value @value end |
Class Method Details
.from_h(hash) ⇒ Contains
Deserializes a Contains rule from a plain Hash.
41 42 43 44 45 |
# File 'lib/inquirex/rules/contains.rb', line 41 def self.from_h(hash) field = hash["field"] || hash[:field] value = hash["value"] || hash[:value] new(field, value) end |
Instance Method Details
#evaluate(answers) ⇒ Boolean
True when the field's answer, coerced to an Array, includes the value.
23 24 25 |
# File 'lib/inquirex/rules/contains.rb', line 23 def evaluate(answers) Array(answers[@field]).include?(@value) end |
#to_h ⇒ Hash{String => Object}
Returns wire format, same shape .from_h accepts.
28 29 30 |
# File 'lib/inquirex/rules/contains.rb', line 28 def to_h { "op" => "contains", "field" => @field.to_s, "value" => @value } end |
#to_s ⇒ String
Returns human-readable form, e.g. "Business in income_types".
33 34 35 |
# File 'lib/inquirex/rules/contains.rb', line 33 def to_s "#{@value} in #{@field}" end |