Class: FlowEngine::Rules::NotEmpty

Inherits:
Base
  • Object
show all
Defined in:
lib/flowengine/rules/not_empty.rb

Overview

Rule: the answer for the given field is present and not empty (nil or empty? => false).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ NotEmpty

Returns a new instance of NotEmpty.

Parameters:

  • field (Symbol) —

    answer key



12
13
14
15
16
# File 'lib/flowengine/rules/not_empty.rb', line 12

def initialize(field)
  super()
  @field = field
  freeze
end

Instance Attribute Details

#field ⇒ Symbol (readonly)

answer key

Returns:

  • (Symbol) —

    the current value of field



8
9
10
# File 'lib/flowengine/rules/not_empty.rb', line 8

def field
  @field
end

Instance Method Details

#evaluate(answers) ⇒ Boolean

Returns false if nil or empty, true otherwise.

Parameters:

  • answers (Hash) —

    current answers

Returns:

  • (Boolean) —

    false if nil or empty, true otherwise



20
21
22
23
24
25
26
# File 'lib/flowengine/rules/not_empty.rb', line 20

def evaluate(answers)
  val = answers[field]
  return false if val.nil?
  return false if val.respond_to?(:empty?) && val.empty?

  true
end

#to_s ⇒ String

Returns e.g. "name is not empty".

Returns:

  • (String) —

    e.g. "name is not empty"



29
30
31
# File 'lib/flowengine/rules/not_empty.rb', line 29

def to_s
  "#{field} is not empty"
end