class SoberSwag::Compiler::Primitive

Compiles a primitive type. Almost always constructed with the values from {SoberSwag::Nodes::Primitive}.

This works by either generating a swagger primitive definition, or a `$ref` to one with a given identifier.

Constants

DATE_PRIMITIVE

Primitive schema used for ruby `Date` values.

DATE_TIME_PRIMITIVE

Primitive schema used for ruby `DateTime` values.

HASH_PRIMITIVE
SWAGGER_PRIMITIVE_DEFS

Map of types that are considered “primitive types” in the OpenAPI V3 spec.

Attributes

type[R]

Public Class Methods

new(type) click to toggle source

@param type [Class] the swagger-able class to document.

# File lib/sober_swag/compiler/primitive.rb, line 12
def initialize(type)
  @type = type

  raise Error, "#{type.inspect} is not a class!" unless @type.is_a?(Class)
end

Public Instance Methods

named?() click to toggle source

Is the wrapped type a named type, causing us to make a ref?

# File lib/sober_swag/compiler/primitive.rb, line 28
def named?
  type <= SoberSwag::Type::Named
end
ref_name() click to toggle source

@return [String] the schema reference

# File lib/sober_swag/compiler/primitive.rb, line 77
def ref_name
  raise Error, 'is not a type that is named!' if swagger_primitive?

  if type <= SoberSwag::Type::Named
    type.root_alias.identifier
  else
    type.name.gsub('::', '.')
  end
end
swagger_primitive?() click to toggle source

Is this documenting one of the build-in swagger types?

# File lib/sober_swag/compiler/primitive.rb, line 22
def swagger_primitive?
  SWAGGER_PRIMITIVE_DEFS.include?(type)
end
type_hash() click to toggle source

Turn this type into a swagger hash with a proper type key. This is suitable for use as the value of a `schema` key in a definition.

@return [Hash] the schema.

# File lib/sober_swag/compiler/primitive.rb, line 37
def type_hash
  if swagger_primitive?
    SWAGGER_PRIMITIVE_DEFS.fetch(type)
  else
    {
      oneOf: [
        { '$ref'.to_sym => named_ref }
      ]
    }
  end
end

Private Instance Methods

named_ref() click to toggle source
# File lib/sober_swag/compiler/primitive.rb, line 89
def named_ref
  "#/components/schemas/#{ref_name}"
end