Class: Sift::Validate::Primitive

Inherits:
Object
  • Object
show all
Defined in:
lib/sift/validate/primitive.rb

Constant Summary collapse

ERROR_MESSAGES =
{
  non_empty_string: "must be a non-empty string",
  numeric: "must be a number",
  string_or_number: "must be a string or a number",
}

Class Method Summary collapse

Class Method Details

.non_empty_string(value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/sift/validate/primitive.rb', line 11

def non_empty_string(value)
  if !value.is_a?(String)
    "#{ERROR_MESSAGES[:non_empty_string]}, got #{value.class}"
  elsif value.empty?
    empty_string_message(:non_empty_string)
  end
end

.numeric(value) ⇒ Object



19
20
21
22
23
# File 'lib/sift/validate/primitive.rb', line 19

def numeric(value)
  if !value.is_a?(Numeric)
    ERROR_MESSAGES[:numeric]
  end
end

.string_or_number(value) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/sift/validate/primitive.rb', line 25

def string_or_number(value)
  if (value.is_a?(String) && value.empty?)
    return empty_string_message(:string_or_number)
  end

  if value.nil? || !(value.is_a?(String) || value.is_a?(Numeric))
    "#{ERROR_MESSAGES[:string_or_number]}, got #{value.class}"
  end
end