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.

Parameters:

  • field (Symbol, String)

    step id whose answer is checked for presence



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

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) ⇒ NotEmpty

Deserializes a NotEmpty rule from a plain Hash.

Parameters:

  • hash (Hash)

    rule hash with string or symbol keys

Returns:



43
44
45
46
# File 'lib/inquirex/rules/not_empty.rb', line 43

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

Instance Method Details

#evaluate(answers) ⇒ Boolean

True when the field's answer is neither nil nor empty (empty String, Array, Hash, etc. all evaluate false).

Parameters:

  • answers (Hash{Symbol => Object})

    answer context, step_id => value

Returns:

  • (Boolean)


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

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

  true
end

#to_hHash{String => Object}

Returns wire format, same shape .from_h accepts.

Returns:

  • (Hash{String => Object})

    wire format, same shape .from_h accepts



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

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

#to_sString

Returns human-readable form, e.g. "income_types is not empty".

Returns:

  • (String)

    human-readable form, e.g. "income_types is not empty"



35
36
37
# File 'lib/inquirex/rules/not_empty.rb', line 35

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