class TypedConfig::RBIBuilder

Public Class Methods

new() click to toggle source
# File lib/typed_config/rbi_builder.rb, line 11
def initialize
  @structs = T.let(
    {},
    T::Hash[
      String,
      T::Array[{ name: Symbol, type: String }],
    ],
  )
end

Public Instance Methods

add_const(struct, name, type) click to toggle source
# File lib/typed_config/rbi_builder.rb, line 27
def add_const(struct, name, type)
  add_struct(struct)
  T.must(@structs[T.must(struct.name)]).push({ name: name, type: type.name })
end
add_struct(struct) click to toggle source
# File lib/typed_config/rbi_builder.rb, line 22
def add_struct(struct)
  @structs[T.must(struct.name)] ||= []
end
build(schema_const_name) click to toggle source
# File lib/typed_config/rbi_builder.rb, line 33
def build(schema_const_name)
  b = binding
  b.local_variable_set(:schema_const_name, schema_const_name)
  b.local_variable_set(:structs, @structs)
  template_file = File.read(File.join(File.dirname(__FILE__), 'structs.rbi.erb'))
  ERB.new(template_file, trim_mode: '>').result(b)
end

Private Instance Methods

build_consts(consts) click to toggle source
# File lib/typed_config/rbi_builder.rb, line 44
def build_consts(consts)
  consts.inject('') do |prev, const|
    prev + "  const :#{const[:name]}, #{const[:type]}\n"
  end
end