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.

Parameters:

  • field (Symbol, String)

    step id whose answer is inspected

  • value (Object)

    value the answer must include



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

#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) ⇒ Contains

Deserializes a Contains rule from a plain Hash.

Parameters:

  • hash (Hash)

    rule hash with string or symbol keys

Returns:



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.

Parameters:

  • answers (Hash{Symbol => Object})

    answer context, step_id => value

Returns:

  • (Boolean)


23
24
25
# File 'lib/inquirex/rules/contains.rb', line 23

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

#to_hHash{String => Object}

Returns wire format, same shape .from_h accepts.

Returns:

  • (Hash{String => Object})

    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_sString

Returns human-readable form, e.g. "Business in income_types".

Returns:

  • (String)

    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