Class: Skit::JsonSchema::Definitions::ConstType

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/skit/json_schema/definitions/const_type.rb

Overview

Represents a JSON Schema const value type.

This generates a Skit::JsonSchema::Types::Const subclass that matches a specific constant value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name:, value:, nullable: false) ⇒ ConstType

Returns a new instance of ConstType.



30
31
32
33
34
# File 'lib/skit/json_schema/definitions/const_type.rb', line 30

def initialize(class_name:, value:, nullable: false)
  @class_name = class_name
  @value = value
  @nullable = nullable
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



15
16
17
# File 'lib/skit/json_schema/definitions/const_type.rb', line 15

def class_name
  @class_name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



21
22
23
# File 'lib/skit/json_schema/definitions/const_type.rb', line 21

def nullable
  @nullable
end

#valueObject (readonly)

Returns the value of attribute value.



18
19
20
# File 'lib/skit/json_schema/definitions/const_type.rb', line 18

def value
  @value
end

Instance Method Details

#to_sorbet_typeObject



37
38
39
# File 'lib/skit/json_schema/definitions/const_type.rb', line 37

def to_sorbet_type
  nullable ? "T.nilable(#{@class_name})" : @class_name
end

#value_literalObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/skit/json_schema/definitions/const_type.rb', line 49

def value_literal
  case @value
  when String
    @value.inspect
  when Integer, Float
    @value.to_s
  when TrueClass
    "true"
  when FalseClass
    "false"
  else
    # This branch should never be reached due to validation in SchemaAnalyzer
    @value.inspect
  end
end

#with_nullableObject



42
43
44
# File 'lib/skit/json_schema/definitions/const_type.rb', line 42

def with_nullable
  ConstType.new(class_name: @class_name, value: @value, nullable: true)
end