Module: RaceGuard::IndexIntegrity::ValidationAst

Defined in:
lib/race_guard/index_integrity/validation_ast.rb

Overview

Internal: Parser AST helpers for ModelScanner (Epic 5.1).

Class Method Summary collapse

Class Method Details

.extract_uniqueness_scope(uniq_node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 41

def extract_uniqueness_scope(uniq_node)
  return nil unless uniq_node.is_a?(Parser::AST::Node)
  return nil if uniq_node.type == :true # rubocop:disable Lint/BooleanSymbol
  return nil unless uniq_node.type == :hash

  scope_from_uniqueness_options(uniq_node)
end

.find_uniqueness_value(hashes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 24

def find_uniqueness_value(hashes)
  hashes.each do |h|
    hash_pairs(h).each do |pair|
      key, val = pair.children
      next unless key.is_a?(Parser::AST::Node) && key.type == :sym
      next unless key.children[0] == :uniqueness

      return val
    end
  end
  nil
end

.hash_pairs(hash_node) ⇒ Object



49
50
51
52
53
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 49

def hash_pairs(hash_node)
  return [] unless hash_node.is_a?(Parser::AST::Node) && hash_node.type == :hash

  hash_node.children.select { |c| c.is_a?(Parser::AST::Node) && c.type == :pair }
end

.literal_to_field_sym(node) ⇒ Object



55
56
57
58
59
60
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 55

def literal_to_field_sym(node)
  case node&.type
  when :sym then node.children[0]
  when :str then node.children[0].to_sym
  end
end

.split_validates_arguments(args) ⇒ Object



17
18
19
20
21
22
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 17

def split_validates_arguments(args)
  fields = []
  hashes = []
  args.each { |arg| break unless consume_validates_argument!(arg, fields, hashes) }
  [fields.compact, hashes]
end

.uniqueness_disabled?(uniq_node) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 37

def uniqueness_disabled?(uniq_node)
  uniq_node.is_a?(Parser::AST::Node) && uniq_node.type == :false # rubocop:disable Lint/BooleanSymbol
end

.validates_send?(node) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
# File 'lib/race_guard/index_integrity/validation_ast.rb', line 8

def validates_send?(node)
  return false unless node.is_a?(Parser::AST::Node) && node.type == :send

  recv, mid, = node.children
  return false unless mid == :validates

  recv.nil? || (recv.is_a?(Parser::AST::Node) && recv.type == :self)
end