Class: Marty::DataGrid::Constraint

Inherits:
Object
  • Object
show all
Defined in:
app/services/marty/data_grid/constraint.rb

Class Method Summary collapse

Class Method Details

.check_data(data_type, data, chks, cvt: false) ⇒ Object

if check_data is called from validation, data has already been converted if called directly from DataGridView, it is still array of array of string.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/marty/data_grid/constraint.rb', line 46

def self.check_data(data_type, data, chks, cvt: false)
  dt = Marty::DataGrid.convert_data_type(data_type)
  klass = dt.class == Class ? dt : DataGrid.maybe_get_klass(dt)
  rt = real_type(data_type) # get real type for string, trueclass etc
  res = []
  pt = 'infinity'
  (0...data.first.length).each do |x|
    (0...data.length).each do |y|
      data_v = data[y][x]
      cvt_val = nil
      err = nil
      begin
        cvt_val = cvt && !data_v.class.in?(rt) ?
                    [DataGrid.parse_fvalue(pt, data_v, dt, klass, false)].
                      flatten.first : data_v
      rescue StandardError => e
        err = e.message
      end
      next res << [:type, x, y] if err

      res << [:constraint, x, y] unless
        cvt_val.nil? ||
        chks.map { |op, chk_val| cvt_val.send(op, chk_val) }.all? { |v| v }
    end
  end
  res
end

.parse(data_type, constraint) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/marty/data_grid/constraint.rb', line 4

def self.parse(data_type, constraint)
  return [] unless constraint

  dt = DataGrid.convert_data_type(data_type)
  if /[><]/.match?(constraint)
    raise "range constraint not allowed for type #{dt}" unless
      ['integer', 'float'].include?(dt)

    pgr = Marty::Util.human_to_pg_range(constraint)
    r = DataGrid.parse_range(pgr)
    [r[0, 2], r[2..-1].reverse]
  else
    raw_vals = constraint.split('|')
    return if raw_vals.blank?
    raise 'list constraint not allowed for type Float' if dt == 'float'

    pt = 'infinity'
    vals = raw_vals.map do |v|
      DataGrid.parse_fvalue(pt, v, data_type, dt, false)
    end
    [[:in?, vals.flatten]]
  end
end

.real_type(data_type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/marty/data_grid/constraint.rb', line 28

def self.real_type(data_type)
  types = case data_type
          when nil
            [DEFAULT_DATA_TYPE.capitalize.constantize]
          when 'string', 'integer', 'float'
            [data_type.capitalize.constantize]
          when 'boolean'
            [TrueClass, FalseClass]
          else
            [data_type]
          end
  types << Integer if types.include?(Float)
  types
end