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.



75
76
77
# File 'lib/solargraph/api_map/constants.rb', line 75

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

#collect(*gates) ⇒ Array<Pin::Base>

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

Parameters:

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

Returns:



38
39
40
41
# File 'lib/solargraph/api_map/constants.rb', line 38

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)


30
31
32
# File 'lib/solargraph/api_map/constants.rb', line 30

def dereference pin
  resolve(pin.name, pin.reference_gates)
end

#qualify(tag, context_tag = '') ⇒ String?

Determine fully qualified tag for a given tag used inside the definition of another tag (“context”). This method will start the search in the specified context until it finds a match for the tag.

Does not recurse into qualifying the type parameters, but returns any which were passed in unchanged.

Parameters:

  • tag (String, nil)

    The namespace to match, complete with generic parameters set to appropriate values if available

  • context_tag (String) (defaults to: '')

    The fully qualified context in which the tag was referenced; start from here to resolve the name. Should not be prefixed with ‘::’.

Returns:

  • (String, nil)

    fully qualified tag



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/solargraph/api_map/constants.rb', line 58

def qualify tag, context_tag = ''
  return tag if ['Boolean', 'self', nil].include?(tag)

  type = ComplexType.try_parse(tag)
  return unless type.defined?
  return tag if type.literal?

  context_type = ComplexType.try_parse(context_tag)
  return unless context_type.defined?

  fqns = qualify_namespace(type.rooted_namespace, context_type.rooted_namespace)
  return unless fqns

  fqns + type.substring
end

#resolve(name, *gates) ⇒ String?

Resolve a name to a fully qualified namespace or constant.

Parameters:

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

Returns:

  • (String, nil)


18
19
20
21
22
23
24
# File 'lib/solargraph/api_map/constants.rb', line 18

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?
  cached_resolve[[name, flat]] || resolve_and_cache(name, flat)
end