Module: Structure::RBS

Defined in:
lib/structure/rbs.rb

Overview

Generates RBS type signatures for Structure classes

Note: Custom methods defined in Structure blocks are not included and must be manually added to RBS files. This is consistent with how Ruby’s RBS tooling handles Data classes.

Class Method Summary collapse

Class Method Details

.emit(klass) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/structure/rbs.rb', line 13

def emit(klass)
  return unless klass < Data

  class_name = klass.name
  return unless class_name

  # @type var meta: Hash[Symbol, untyped]
  meta = klass.respond_to?(:__structure_meta__) ? klass.__structure_meta__ : {}

  attributes = meta[:mappings] ? meta[:mappings].keys : klass.members
  types = meta.fetch(:types, {}) # steep:ignore
  required = meta.fetch(:required, attributes) # steep:ignore

  emit_rbs_content(
    class_name:,
    attributes:,
    types:,
    required:,
    has_structure_modules: meta.any?,
  )
end

.write(klass, dir: "sig") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/structure/rbs.rb', line 35

def write(klass, dir: "sig")
  rbs_content = emit(klass)
  return unless rbs_content

  # User::Address -> user/address.rbs
  path_segments = klass.name.split("::").map(&:downcase)
  filename = "#{path_segments.pop}.rbs"

  # full path
  dir_path = Pathname.new(dir)
  dir_path = dir_path.join(*path_segments) unless path_segments.empty?
  FileUtils.mkdir_p(dir_path)

  file_path = dir_path.join(filename).to_s
  File.write(file_path, rbs_content)

  file_path
end