Class: Inquirex::Rules::Equals

Inherits:
Base
  • Object
show all
Defined in:
lib/inquirex/rules/equals.rb

Overview

Rule: the answer for the given field equals the given value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, value) ⇒ Equals

Returns a new instance of Equals.

Parameters:

  • field (Symbol, String)

    step id whose answer is compared

  • value (Object)

    value the answer must equal



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

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

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



7
8
9
# File 'lib/inquirex/rules/equals.rb', line 7

def field
  @field
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/inquirex/rules/equals.rb', line 7

def value
  @value
end

Class Method Details

.from_h(hash) ⇒ Equals

Deserializes an Equals rule from a plain Hash.

Parameters:

  • hash (Hash)

    rule hash with string or symbol keys

Returns:



40
41
42
43
44
# File 'lib/inquirex/rules/equals.rb', line 40

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 equals the value (Ruby ==).

Parameters:

  • answers (Hash{Symbol => Object})

    answer context, step_id => value

Returns:

  • (Boolean)


22
23
24
# File 'lib/inquirex/rules/equals.rb', line 22

def evaluate(answers)
  answers[@field] == @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



27
28
29
# File 'lib/inquirex/rules/equals.rb', line 27

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

#to_sString

Returns human-readable form, e.g. "filing_status == single".

Returns:

  • (String)

    human-readable form, e.g. "filing_status == single"



32
33
34
# File 'lib/inquirex/rules/equals.rb', line 32

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