Sha256: a14a257a12615f05ff0a8a4db8b55133a9b22cd02bcb7d2a9013c3753b3d6adb

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Cocina
  module Generator
    # Base class for generating from openapi
    class SchemaBase
      attr_reader :schema_doc, :key, :required, :nullable, :parent, :relaxed

      def initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil, relaxed: false)
        @schema_doc = schema_doc
        @key = key
        @required = required
        @nullable = nullable
        @parent = parent
        @relaxed = relaxed
      end

      def filename
        "#{name.underscore}.rb"
      end

      def name
        key || schema_doc.name
      end

      # Allows nillable values to be set to nil. This is useful when doing
      # an update and you want to clear out a value.
      def optional
        nullable || relaxed ? '.optional' : ''
      end

      def quote(item)
        return item unless schema_doc.type == 'string'

        "'#{item}'"
      end

      def description
        return '' unless schema_doc.description

        "# #{schema_doc.description}\n"
      end

      def example
        return '' unless schema_doc.example

        "# example: #{schema_doc.example}\n"
      end

      def relaxed_comment
        return '' unless relaxed

        "# Validation of this property is relaxed. See the openapi for full validation.\n"
      end

      def dry_datatype(doc)
        case doc.type
        when 'integer'
          'Strict::Integer'
        when 'string'
          string_dry_datatype(doc)
        when 'boolean'
          'Strict::Bool'
        else
          if any_datatype?(doc)
            'Nominal::Any'
          else
            raise "#{schema_doc.type} not supported"
          end
        end
      end

      def any_datatype?(doc)
        relaxed || doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
      end

      def string_dry_datatype(doc)
        case doc.format
        when 'date-time'
          'Params::DateTime'
        else
          'Strict::String'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cocina-models-0.79.0 lib/cocina/generator/schema_base.rb
cocina-models-0.78.0 lib/cocina/generator/schema_base.rb
cocina-models-0.77.0 lib/cocina/generator/schema_base.rb
cocina-models-0.76.0 lib/cocina/generator/schema_base.rb