Sha256: 42c419670bb8c6c01d2da28938f5d0759b022f00147377ceef9cc482577336b0

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

module SoberSwag
  class Compiler
    ##
    # Compiles a primitive type.
    # Almost always constructed with the values from
    # {SoberSwag::Nodes::Primitive}.
    class Primitive
      def initialize(type)
        @type = type

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

      attr_reader :type

      def swagger_primitive?
        SWAGGER_PRIMITIVE_DEFS.include?(type)
      end

      ##
      # Is the wrapped type a named type, causing us to make a ref?
      def named?
        type <= SoberSwag::Type::Named
      end

      ##
      # Turn this type into a swagger hash with a proper type key.
      def type_hash
        if swagger_primitive?
          SWAGGER_PRIMITIVE_DEFS.fetch(type)
        else
          {
            oneOf: [
              { '$ref'.to_sym => named_ref }
            ]
          }
        end
      end

      DATE_PRIMITIVE = { type: :string, format: :date }.freeze
      DATE_TIME_PRIMITIVE = { type: :string, format: :'date-time' }.freeze

      SWAGGER_PRIMITIVE_DEFS =
        {
          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
        ).transform_values(&:freeze).freeze

      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

      private

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sober_swag-0.17.0 lib/sober_swag/compiler/primitive.rb
sober_swag-0.16.0 lib/sober_swag/compiler/primitive.rb
sober_swag-0.15.0 lib/sober_swag/compiler/primitive.rb