Class: Inquirex::Rules::NotEmpty

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

Overview

Rule: the answer for the given field is present and not empty.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ NotEmpty

Returns a new instance of NotEmpty.



9
10
11
12
13
# File 'lib/inquirex/rules/not_empty.rb', line 9

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

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



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

def field
  @field
end

Class Method Details

.from_h(hash) ⇒ Object



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

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

Instance Method Details

#evaluate(answers) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/inquirex/rules/not_empty.rb', line 15

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

  true
end

#to_hObject



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

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

#to_sObject



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

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