123456789_123456789_123456789_123456789_123456789_

Class: RBS::Prototype::Runtime::StructGenerator

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RBS::Prototype::Runtime::ValueObjectBase
Defined in: lib/rbs/prototype/runtime/value_object_generator.rb

Constant Summary

Class Method Summary

Instance Method Summary

ValueObjectBase - Inherited

#build_decl,
#build_member_accessors

attr_accessor foo: untyped.

#build_s_members

def self.members: () -> [ :foo, :bar ] def members: () -> [ :foo, :bar ].

Helpers - Included

#const_name, #const_name!,
#only_name

Returns the exact name & not compactly declared name.

#to_type_name, #untyped

Constructor Details

This class inherits a constructor from RBS::Prototype::Runtime::ValueObjectBase

Class Method Details

.generatable?(target) ⇒ Boolean

[ GitHub ]

  
# File '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

Instance Method Details

#add_decl_members(decl) (private)

[ GitHub ]

  
# File '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 (private)

[ GitHub ]

  
# File '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 (private)

[ GitHub ]

  
# File '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 (private)

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

[ GitHub ]

  
# File '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 (private)

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

| (?foo: untyped, ?bar: untyped) -> instance
[ GitHub ]

  
# File '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 (private)

[ GitHub ]

  
# File '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