class TypedConfig::SchemaBuilder

Attributes

rbi_builder[R]
struct[R]

Public Class Methods

new(struct, rbi_builder: RBIBuilder.new) click to toggle source
# File lib/typed_config/schema_builder.rb, line 12
def initialize(struct, rbi_builder: RBIBuilder.new)
  @rbi_builder = rbi_builder
  @struct = T.let(create_struct(Structs, struct), Class)
end

Public Instance Methods

build_rbi(schema_const_name) click to toggle source
# File lib/typed_config/schema_builder.rb, line 35
def build_rbi(schema_const_name)
  rbi_builder.build(schema_const_name)
end
const(name, type = nil, &blk) click to toggle source
# File lib/typed_config/schema_builder.rb, line 24
def const(name, type = nil, &blk)
  clazz = retrieve_class(name: name, type: type, &blk)
  raise ArgumentError, "Attribute '#{name}' must receive a type or block." if clazz.nil?

  rbi_builder.add_const(struct, name, clazz)
  struct.tap do |scope|
    scope.const name, clazz
  end
end

Protected Instance Methods

create_struct(base, name) click to toggle source
# File lib/typed_config/schema_builder.rb, line 71
def create_struct(base, name)
  new_struct = base.const_set(name.camelcase, Class.new(T::Struct))
  rbi_builder.add_struct(new_struct)
  new_struct
end
process_substruct(name:, &blk) click to toggle source
# File lib/typed_config/schema_builder.rb, line 61
def process_substruct(name:, &blk)
  top_struct = struct
  sub_struct = create_struct(struct, name.to_s)
  @struct = sub_struct
  instance_eval(&blk)
  @struct = top_struct
  sub_struct
end
retrieve_class(name:, type:, &blk) click to toggle source
# File lib/typed_config/schema_builder.rb, line 54
def retrieve_class(name:, type:, &blk)
  return type unless block_given?

  process_substruct(name: name, &blk)
end