Class: Inquirex::Rules::Contains

Inherits:
Base
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, value) ⇒ Contains

Returns a new instance of Contains.



10
11
12
13
14
15
# File 'lib/inquirex/rules/contains.rb', line 10

def initialize(field, value)
  super()
  @field = field.to_sym
  @value = value
  freeze
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'lib/inquirex/rules/contains.rb', line 8

def field
  @field
end

#valueObject (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) ⇒ Object



29
30
31
32
33
# File 'lib/inquirex/rules/contains.rb', line 29

def self.from_h(hash)
  field = hash["field"] || hash[:field]
  value = hash["value"] || hash[:value]
  new(field, value)
end

Instance Method Details

#evaluate(answers) ⇒ Object



17
18
19
# File 'lib/inquirex/rules/contains.rb', line 17

def evaluate(answers)
  Array(answers[@field]).include?(@value)
end

#to_hObject



21
22
23
# File 'lib/inquirex/rules/contains.rb', line 21

def to_h
  { "op" => "contains", "field" => @field.to_s, "value" => @value }
end

#to_sObject



25
26
27
# File 'lib/inquirex/rules/contains.rb', line 25

def to_s
  "#{@value} in #{@field}"
end