Class: SoberSwag::Compiler::Primitive

Inherits:
Object
  • Object
show all
Defined in:
lib/sober_swag/compiler/primitive.rb

Overview

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

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

Constant Summary collapse

DATE_PRIMITIVE =

Primitive schema used for ruby Date values.

{ type: :string, format: :date }.freeze
DATE_TIME_PRIMITIVE =

Primitive schema used for ruby DateTime values.

{ type: :string, format: :'date-time' }.freeze
HASH_PRIMITIVE =
{ type: :object, additionalProperties: true }.freeze
SWAGGER_PRIMITIVE_DEFS =

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

{
  NilClass => :null,
  TrueClass => :boolean,
  FalseClass => :boolean,
  Float => :number,
  Integer => :integer,
  String => :string
}.transform_values { |v| { type: v.freeze } }
.to_h.merge(
  Date => DATE_PRIMITIVE,
  DateTime => DATE_TIME_PRIMITIVE,
  Time => DATE_TIME_PRIMITIVE,
  Hash => HASH_PRIMITIVE
).transform_values(&:freeze).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Primitive

Returns a new instance of Primitive.

Parameters:

  • type (Class)

    the swagger-able class to document.

Raises:



12
13
14
15
16
# 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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/sober_swag/compiler/primitive.rb', line 18

def type
  @type
end

Instance Method Details

#named?Boolean

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/sober_swag/compiler/primitive.rb', line 28

def named?
  type <= SoberSwag::Type::Named
end

#named_refObject (private)



89
90
91
# File 'lib/sober_swag/compiler/primitive.rb', line 89

def named_ref
  "#/components/schemas/#{ref_name}"
end

#ref_nameString

Returns the schema reference.

Returns:

  • (String)

    the schema reference

Raises:



77
78
79
80
81
82
83
84
85
# 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?Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/sober_swag/compiler/primitive.rb', line 22

def swagger_primitive?
  SWAGGER_PRIMITIVE_DEFS.include?(type)
end

#type_hashHash

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.

Returns:

  • (Hash)

    the schema.



37
38
39
40
41
42
43
44
45
46
47
# 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