Module: JSONdb::Validations::Types

Includes:
Logger
Included in:
Field, Record, Records
Defined in:
lib/jsondb/validations.rb

Constant Summary collapse

AllowedTypes =
[
  "String", 
  "Fixnum", 
  "Integer", 
  "Float", 
  "Time", 
  "Bool"
]

Instance Method Summary collapse

Methods included from Logger

#allowed_log_level?, #log, #log_enabled?, #log_this?

Instance Method Details

#allowed_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/jsondb/validations.rb', line 32

def allowed_type?(type)
  if AllowedTypes.include?(type.to_s)
    return true
  else
    log("Validation: Type '#{type.to_s}' not allowed", :error)
    return false
  end
end

#validate_type(_class, _value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsondb/validations.rb', line 41

def validate_type(_class, _value)
  return true if _value.nil?

  case _class
  when "Bool"
    bool?(_class, _value)
  when "String"
    string?(_class, _value)
  when "Fixnum", "Integer", "Float"
    numeric?(_class, _value)
  when "Time"
    time?(_class, _value)
  else
    log("Validation: '#{_class}' type not allowed", :error)
    return false
  end
end