Class: Inquirex::Rules::NotEmpty
- 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
-
#field ⇒ Object
readonly
Returns the value of attribute field.
Class Method Summary collapse
-
.from_h(hash) ⇒ NotEmpty
Deserializes a NotEmpty rule from a plain Hash.
Instance Method Summary collapse
-
#evaluate(answers) ⇒ Boolean
True when the field's answer is neither nil nor empty (empty String, Array, Hash, etc. all evaluate false).
-
#initialize(field) ⇒ NotEmpty
constructor
A new instance of NotEmpty.
-
#to_h ⇒ Hash{String => Object}
Wire format, same shape .from_h accepts.
-
#to_s ⇒ String
Human-readable form, e.g.
Constructor Details
#initialize(field) ⇒ NotEmpty
Returns a new instance of NotEmpty.
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
#field ⇒ Object (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.
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).
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_h ⇒ Hash{String => Object}
Returns 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_s ⇒ String
Returns 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 |