Class: Jade::Api

Inherits:
Data
  • Object
show all
Defined in:
lib/jade/api.rb

Overview

The public surface of everything a module can call, read from the registry rather than the source. Stdlib modules are written both as Jade heredocs and as a Ruby DSL, and an extension gem's modules aren't in the tree at all, so no grep of the source finds them.

Constant Summary collapse

ORIGIN_ORDER =
{ 'project' => 0, 'extension' => 1, 'stdlib' => 2 }.freeze
KIND_ORDER =
{
  'type' => 0,
  'struct' => 0,
  'interface' => 1,
  'constructor' => 2,
  'function' => 3,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry

Returns:

  • (Object)

    the current value of registry



10
11
12
# File 'lib/jade/api.rb', line 10

def registry
  @registry
end

#skippedObject (readonly)

Returns the value of attribute skipped

Returns:

  • (Object)

    the current value of skipped



10
11
12
# File 'lib/jade/api.rb', line 10

def skipped
  @skipped
end

#source_rootObject (readonly)

Returns the value of attribute source_root

Returns:

  • (Object)

    the current value of source_root



10
11
12
# File 'lib/jade/api.rb', line 10

def source_root
  @source_root
end

Class Method Details

.load(project = Project.find) ⇒ Object



11
12
13
# File 'lib/jade/api.rb', line 11

def self.load(project = Project.find)
  project ? of_project(project) : stdlib
end

.stdlibObject



15
16
17
# File 'lib/jade/api.rb', line 15

def self.stdlib
  new(registry: stdlib_registry, source_root: nil, skipped: [])
end

.stdlib_registryObject



19
20
21
# File 'lib/jade/api.rb', line 19

def self.stdlib_registry
  @stdlib_registry ||= Stdlib.load(Registry.new)
end

Instance Method Details

#describe(module_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jade/api.rb', line 85

def describe(module_name)
  registry
    .modules[module_name]
    &.then do
      {
        module: module_name,
        origin: origin(it),
        symbols: entries_for(module_name, it),
      }
    end
end

#lookup(qualified_name) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/jade/api.rb', line 97

def lookup(qualified_name)
  *module_parts, name = qualified_name.split('.')
  module_parts
    .join('.')
    .then { describe(it) }
    &.fetch(:symbols)
    &.find { it[:name] == name }
end

#modulesObject



78
79
80
81
82
83
# File 'lib/jade/api.rb', line 78

def modules
  registry
    .modules
    .map { |name, entry| { name:, origin: origin(entry) } }
    .sort_by { [ORIGIN_ORDER.fetch(it[:origin]), it[:name]] }
end

#search(term) ⇒ Object



106
107
108
109
110
111
# File 'lib/jade/api.rb', line 106

def search(term)
  registry
    .modules
    .flat_map { |name, entry| entries_for(name, entry) }
    .select { it[:qualified_name].downcase.include?(term.downcase) }
end