class RBS::Prototype::Runtime::StructGenerator

Constants

CAN_CALL_KEYWORD_INIT_P

Public Class Methods

generatable?(target) click to toggle source
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 90
def self.generatable?(target)
  return false unless target < Struct
  # Avoid direct inherited class like `class Option < Struct`
  return false unless target.respond_to?(:members)

  true
end

Private Instance Methods

add_decl_members(decl) click to toggle source
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 106
def add_decl_members(decl)
  decl.members.concat build_s_new
  decl.members.concat build_s_keyword_init_p
  decl.members.concat build_s_members
  decl.members.concat build_member_accessors(AST::Members::AttrAccessor)
end
build_overload_for_keyword_arguments() click to toggle source
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 163
def build_overload_for_keyword_arguments
  AST::Members::MethodDefinition::Overload.new(
    annotations: [],
    method_type: MethodType.new(
      type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
        optional_keywords: @target_class.members.to_h { |m| [m, Types::Function::Param.new(name: nil, type: untyped)] },
      ),
      type_params: [],
      block: nil,
      location: nil,
    )
  )
end
build_overload_for_positional_arguments() click to toggle source
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 149
def build_overload_for_positional_arguments
  AST::Members::MethodDefinition::Overload.new(
    annotations: [],
    method_type: MethodType.new(
      type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
        optional_positionals: @target_class.members.map { |m| Types::Function::Param.new(name: m, type: untyped) },
      ),
      type_params: [],
      block: nil,
      location: nil,
    )
  )
end
build_s_keyword_init_p() click to toggle source

def self.keyword_init?: () -> bool?

# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 178
def build_s_keyword_init_p
  return [] unless CAN_CALL_KEYWORD_INIT_P

  return_type = @target_class.keyword_init?.nil? \
              ? Types::Bases::Nil.new(location: nil)
              : Types::Literal.new(literal: @target_class.keyword_init?, location: nil)
  type = Types::Function.empty(return_type)

  [
    AST::Members::MethodDefinition.new(
      name: :keyword_init?,
      overloads: [
        AST::Members::MethodDefinition::Overload.new(
          annotations: [],
          method_type: MethodType.new(
            type: type,
            type_params: [],
            block: nil,
            location: nil,
          )
        )
      ],
      kind: :singleton,
      location: nil,
      comment: nil,
      annotations: [],
      overloading: false,
      visibility: nil
    )
  ]
end
build_s_new() click to toggle source

def self.new: (?untyped foo, ?untyped bar) -> instance

| (?foo: untyped, ?bar: untyped) -> instance
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 115
def build_s_new
  [:new, :[]].map do |name|
    new_overloads = []

    if CAN_CALL_KEYWORD_INIT_P
      case @target_class.keyword_init?
      when false
        new_overloads << build_overload_for_positional_arguments
      when true
        new_overloads << build_overload_for_keyword_arguments
      when nil
        new_overloads << build_overload_for_positional_arguments
        new_overloads << build_overload_for_keyword_arguments
      else
        raise
      end
    else
      new_overloads << build_overload_for_positional_arguments
      new_overloads << build_overload_for_keyword_arguments
    end

    AST::Members::MethodDefinition.new(
      name: name,
      overloads: new_overloads,
      kind: :singleton,
      location: nil,
      comment: nil,
      annotations: [],
      overloading: false,
      visibility: nil
    )
  end
end
build_super_class() click to toggle source
# File rbs-3.6.1/lib/rbs/prototype/runtime/value_object_generator.rb, line 102
def build_super_class
  AST::Declarations::Class::Super.new(name: TypeName("::Struct"), args: [untyped], location: nil)
end