Class: RubyBindgen::Symbols

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bindgen/symbols.rb

Overview

Skip / version-guard / FFI-override decisions for cursors, looked up by name strings supplied in the YAML symbols config.

Owns the storage (an exact-match hash plus a list of regex entries) and the policy queries (skip?, version, override). Delegates name enumeration to SymbolCandidates so the matching logic is shared with Namer / NameMapper.

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Symbols

Returns a new instance of Symbols.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby-bindgen/symbols.rb', line 10

def initialize(config = {})
  @exact = {}
  @regex = []

  (config[:skip] || []).each do |name|
    add_entry(name, skip: true)
  end

  (config[:versions] || {}).each do |version, names|
    names.each do |name|
      add_entry(name, version: version)
    end
  end

  (config[:overrides] || {}).each do |name, signature|
    add_entry(name.to_s, signature: signature)
  end
end

Instance Method Details

#has_versions?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ruby-bindgen/symbols.rb', line 86

def has_versions?
  @exact.any? { |_, entry| entry.version } || @regex.any? { |_, entry| entry.version }
end

#lookup(candidates) ⇒ Object

Look up a list of pre-built candidate names. Returns a SymbolEntry or nil.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby-bindgen/symbols.rb', line 38

def lookup(candidates)
  candidates.each do |name|
    result = @exact[SymbolCandidates.normalize_signature(name)]
    return result if result
  end

  @regex.each do |pattern, entry|
    candidates.each do |name|
      return entry if pattern.match?(SymbolCandidates.normalize_signature(name))
    end
  end
  nil
end

#lookup_cursor(cursor) ⇒ Object

Look up a cursor by trying each of its candidate names. Returns a SymbolEntry or nil.



31
32
33
34
# File 'lib/ruby-bindgen/symbols.rb', line 31

def lookup_cursor(cursor)
  symbol_candidates = SymbolCandidates.new(cursor)
  lookup(symbol_candidates)
end

#override(cursor) ⇒ Object

Returns the override signature string for a cursor, or nil if not overridden.



81
82
83
84
# File 'lib/ruby-bindgen/symbols.rb', line 81

def override(cursor)
  entry = lookup_cursor(cursor)
  entry&.signature
end

#skip?(cursor) ⇒ Boolean

Check if a cursor should be skipped based on symbols config.

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/ruby-bindgen/symbols.rb', line 53

def skip?(cursor)
  entry = lookup_cursor(cursor)
  entry&.skip? || false
end

#skip_spelling?(spelling) ⇒ Boolean

Check if a type spelling matches any skip symbol using word boundaries. Used as a fallback for dependent/unexposed types where no declaration is available.

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby-bindgen/symbols.rb', line 61

def skip_spelling?(spelling)
  @exact.each do |key, entry|
    next unless entry.skip?
    simple_name = key.split('::').last
    return true if spelling.match?(/\b#{Regexp.escape(simple_name)}\b/)
  end
  @regex.each do |pattern, entry|
    next unless entry.skip?
    return true if pattern.match?(spelling)
  end
  false
end

#version(cursor) ⇒ Object

Returns the version guard value for a cursor, or nil if not version-guarded.



75
76
77
78
# File 'lib/ruby-bindgen/symbols.rb', line 75

def version(cursor)
  entry = lookup_cursor(cursor)
  entry&.version
end