Class: Solargraph::ApiMap::Constants

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/api_map/constants.rb

Overview

Methods for handling constants.

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Constants

Returns a new instance of Constants.

Parameters:



9
10
11
# File 'lib/solargraph/api_map/constants.rb', line 9

def initialize store
  @store = store
end

Instance Method Details

#clearvoid

This method returns an undefined value.



97
98
99
# File 'lib/solargraph/api_map/constants.rb', line 97

def clear
  [cached_collect, cached_resolve].each(&:clear)
end

#collect(*gates) ⇒ Array<Solargraph::Pin::Namespace, Solargraph::Pin::Constant>

Collect a list of all constants defined in the specified gates.

Parameters:

  • gates (Array<Array<String>, String>)

Returns:



58
59
60
61
# File 'lib/solargraph/api_map/constants.rb', line 58

def collect(*gates)
  flat = gates.flatten
  cached_collect[flat] || collect_and_cache(flat)
end

#dereference(pin) ⇒ String?

Get a fully qualified namespace from a reference pin.

Parameters:

Returns:

  • (String, nil)


50
51
52
# File 'lib/solargraph/api_map/constants.rb', line 50

def dereference pin
  qualify_type(pin.type, *pin.reference_gates)&.tag
end

#qualify(tag, *gates) ⇒ String?

Determine a fully qualified namespace for a given tag referenced from the specified open gates. This method will search in each gate until it finds a match for the name.

Parameters:

  • tag (String, nil)

    The type to match

  • gates (Array<String>)

Returns:

  • (String, nil)

    fully qualified tag



70
71
72
73
# File 'lib/solargraph/api_map/constants.rb', line 70

def qualify tag, *gates
  type = ComplexType.try_parse(tag)
  qualify_type(type, *gates)&.tag
end

#qualify_type(type, *gates) ⇒ ComplexType?

Returns A new rooted ComplexType.

Parameters:

  • type (ComplexType, nil)

    The type to match

  • gates (Array<String>)

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solargraph/api_map/constants.rb', line 79

def qualify_type type, *gates
  return nil if type.nil?
  return type if type.selfy? || type.literal? || type.tag == 'nil' || type.interface? ||
                 type.tag == 'Boolean'

  gates.push '' unless gates.include?('')
  fqns = resolve(type.rooted_namespace, *gates)
  return unless fqns
  pin = store.get_path_pins(fqns).first
  if pin.is_a?(Pin::Constant)
    const = Solargraph::Parser::NodeMethods.unpack_name(pin.assignment)
    return unless const
    fqns = resolve(const, *pin.gates)
  end
  type.recreate(new_name: fqns, make_rooted: true)
end

#resolve(name, *gates) ⇒ String?

Resolve a name to a fully qualified namespace or constant.

‘Constants#resolve` finds fully qualified (absolute) namespaces based on relative names and the open gates (namespaces) provided. Names must be runtime-visible (erased) non-literal types, non-duck, non-signature types - e.g., TrueClass, NilClass, Integer and Hash instead of true, nil, 96, or Hash=> Symbol

Note: You may want to be using #qualify. Notably, #resolve:

  • does not handle anything with type parameters

  • will not gracefully handle nil, self and Boolean

  • will return a constant name instead of following its assignment

Parameters:

  • name (String)

    Namespace which may relative and not be rooted.

  • gates (Array<Array<String>, String>)

    Namespaces to search while resolving the name

Returns:

  • (String, nil)

    fully qualified namespace (i.e., is absolute, but will not start with ::)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/solargraph/api_map/constants.rb', line 32

def resolve(name, *gates)
  return store.get_path_pins(name[2..]).first&.path if name.start_with?('::')

  flat = gates.flatten
  flat.push '' if flat.empty?
  if cached_resolve.include? [name, flat]
    cached_result = cached_resolve[[name, flat]]
    # don't recurse

    return nil if cached_result == :in_process
    return cached_result
  end
  resolve_and_cache(name, flat)
end