Class: Taupe::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/taupe/model/validate.rb

Overview

Validator class

Class Method Summary collapse

Class Method Details

.check(values, definitions) ⇒ Object

Check data integrity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/taupe/model/validate.rb', line 12

def self.check(values, definitions)
  errors = []
  definitions.each do |name, props|
    can_be_null = props[:null] || true
    if values.include?(name)
      value = values[name]
      expected_type = props[:type] || String
      unless value.is_a? expected_type
        errors << "#{name} (#{value.class.name}) must be a #{expected_type}"
      end
    else
      errors << "#{name} can not be null" unless can_be_null
    end
  end

  fail errors.join(' - ') unless errors.empty?

  values
end

.standardize_sql_type(sql_type) ⇒ Object

Transform a SQL type into a standard type



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/taupe/model/validate.rb', line 33

def self.standardize_sql_type(sql_type)
  standard_type = nil
  case sql_type.to_s.downcase
  when 'integer', 'int', 'int(11)', 'bigint', 'smallint', 'tinyint'
    standard_type = Integer
  when 'float'
    standard_type = Float
  when 'date', 'time', 'datetime', 'timestamp',
    'timestamp wit time zone', 'timestamp without time zone'
    standard_type = Time
  else
    standard_type = String
  end

  standard_type
end