Class: Marty::DataGrid::DataGridValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/marty/data_grid.rb

Instance Method Summary collapse

Instance Method Details

#validate(dg) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/marty/data_grid.rb', line 19

def validate(dg)
  dg.errors.add(:base, "'#{dg.data_type}' not a defined type or class") unless
    Marty::DataGrid.convert_data_type(dg.data_type)

  dg.errors.add(:base, 'data must be array of arrays') unless
    dg.data.is_a?(Array) && dg.data.all? { |a| a.is_a? Array }

  dg.errors.add(:base, 'metadata must be an array of hashes') unless
    dg..is_a?(Array) && dg..all? { |a| a.is_a? Hash }

  dg.errors.add(:base, 'metadata must contain only h/v dirs') unless
    dg..all? { |h| ['h', 'v'].member? h['dir'] }

  dg.errors.add(:base, 'metadata item attrs must be unique') unless
    dg..map { |h| h['attr'] }.uniq.length == dg..length

  dg..each do |inf|
    attr, type, keys, rs_keep =
      inf['attr'], inf['type'], inf['keys'], inf['rs_keep']

    if rs_keep.present?
      m = /\A *(<|<=|>|>=)? *([a-z][a-z_0-9]+) *\z/.match(rs_keep)
      unless m
        dg.errors.add(:base, "invalid grid modifier expression: #{rs_keep}")
        next
      end
    end

    dg.errors.add(:base, 'metadata elements must have attr/type/keys') unless
      attr && type && keys

    # enforce Delorean attr syntax (a bit Draconian)
    dg.errors.add(:base, "bad attribute '#{attr}'") unless
      /^[a-z][A-Za-z0-9_]*$/.match?(attr)

    dg.errors.add(:base, "unknown metadata type #{type}") unless
      Marty::DataGrid.type_to_index(type)

    dg.errors.add(:base, 'bad metadata keys') unless
      keys.is_a?(Array) && !keys.empty?
  end

  # Check key uniqueness of vertical/horizontal key
  # combinations. FIXME: ideally, we should also check for
  # array/range key subsumption.  Those will result in runtime
  # errors anyway when multiple hits are produced.
  v_keys = dg.dir_infos('v').map { |inf| inf['keys'] }
  h_keys = dg.dir_infos('h').map { |inf| inf['keys'] }

  v_zip_keys = v_keys.empty? ? [] : v_keys[0].zip(*v_keys[1..-1])
  h_zip_keys = h_keys.empty? ? [] : h_keys[0].zip(*h_keys[1..-1])

  dg.errors.add(:base, 'duplicate horiz. key combination') unless
    h_zip_keys.uniq.length == h_zip_keys.length

  dg.errors.add(:base, 'duplicate vertical key combination') unless
    v_zip_keys.uniq.length == v_zip_keys.length

  con_chk = []
  begin
    con_chk = Marty::DataGrid::Constraint.parse(dg.data_type, dg.constraint)
  rescue StandardError => e
    dg.errors.add(:base, "Error in constraint: #{e.message}")
  end
  data_check = Marty::DataGrid::Constraint.check_data(dg.data_type,
                                                      dg.data, con_chk)
  return if data_check.blank?

  data_check.each do |(err, x, y)|
    dg.errors.add(:base, "cell #{x}, #{y} fails constraint check") if
      err == :constraint
    dg.errors.add(:base, "cell #{x}, #{y} incorrect type") if
      err == :type
  end
end