Class: SoberSwag::Compiler::Primitive
- Inherits:
-
Object
- Object
- SoberSwag::Compiler::Primitive
- 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
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(type) ⇒ Primitive
constructor
A new instance of Primitive.
-
#named? ⇒ Boolean
Is the wrapped type a named type, causing us to make a ref?.
- #named_ref ⇒ Object private
-
#ref_name ⇒ String
The schema reference.
-
#swagger_primitive? ⇒ Boolean
Is this documenting one of the build-in swagger types?.
-
#type_hash ⇒ Hash
Turn this type into a swagger hash with a proper type key.
Constructor Details
Instance Attribute Details
#type ⇒ Object (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?
28 29 30 |
# File 'lib/sober_swag/compiler/primitive.rb', line 28 def named? type <= SoberSwag::Type::Named end |
#named_ref ⇒ Object (private)
89 90 91 |
# File 'lib/sober_swag/compiler/primitive.rb', line 89 def named_ref "#/components/schemas/#{ref_name}" end |
#ref_name ⇒ String
Returns the schema reference.
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?
22 23 24 |
# File 'lib/sober_swag/compiler/primitive.rb', line 22 def swagger_primitive? SWAGGER_PRIMITIVE_DEFS.include?(type) end |
#type_hash ⇒ Hash
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.
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 |