Module: RaceGuard::IndexIntegrity::SchemaAst

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

Overview

Internal: Parser AST helpers for SchemaAnalyzer (Epic 5.2).

Class Method Summary collapse

Class Method Details

.add_index_send?(node) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 91

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

  recv, mid, = node.children
  recv.nil? && mid == :add_index
end

.column_array_to_syms(array_node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 34

def column_array_to_syms(array_node)
  return nil unless array_node.is_a?(Parser::AST::Node) && array_node.type == :array

  cols = array_node.children.grep(Parser::AST::Node).filter_map do |el|
    literal_table_sym(el)
  end
  return nil if cols.size != array_node.children.grep(Parser::AST::Node).size

  cols
end

.create_table_name(send_node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 70

def create_table_name(send_node)
  return nil unless send_node.is_a?(Parser::AST::Node) && send_node.type == :send
  return nil unless send_method(send_node) == :create_table

  args = send_node.children.drop(2)
  return nil if args.empty?

  first = args[0]
  sym = literal_table_sym(first)
  return sym if sym

  nil
end

.hash_has_key?(hash_node, key_sym) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 52

def hash_has_key?(hash_node, key_sym)
  hash_pairs(hash_node).any? do |pair|
    k, = pair.children
    k.is_a?(Parser::AST::Node) && k.type == :sym && k.children[0] == key_sym
  end
end

.hash_name(hash_node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 59

def hash_name(hash_node)
  hash_pairs(hash_node).each do |pair|
    k, v = pair.children
    next unless k.is_a?(Parser::AST::Node) && k.type == :sym
    next unless k.children[0] == :name

    return literal_string_or_sym(v) if v
  end
  nil
end

.hash_option_true?(hash_node, key_sym) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 45

def hash_option_true?(hash_node, key_sym)
  val = pair_value_for_key(hash_node, key_sym)
  return false unless val.is_a?(Parser::AST::Node)

  val.type == :true # rubocop:disable Lint/BooleanSymbol
end

.hash_pairs(hash_node) ⇒ Object



14
15
16
17
18
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 14

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_string_or_sym(node) ⇒ Object



27
28
29
30
31
32
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 27

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

.literal_table_sym(node) ⇒ Object



20
21
22
23
24
25
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 20

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

.send_method(node) ⇒ Object



8
9
10
11
12
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 8

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

  node.children[1]
end

.t_index_send?(node, table_ctx) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/race_guard/index_integrity/schema_ast.rb', line 84

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

  recv, mid, = node.children
  mid == :index && lvar_named?(recv, :t)
end