Sha256: 4372f1d65dc763246c339591af4ddc52a335bca8bd90d9fe92a0a8dac2b5d1b9

Contents?: true

Size: 1.39 KB

Versions: 11

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

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

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

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

      def name
        key || schema_doc.name
      end

      def omittable
        return '' if required

        '.meta(omittable: true)'
      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 dry_datatype(doc)
        case doc.type
        when 'integer'
          'Strict::Integer'
        when 'string'
          string_dry_datatype(doc)
        when 'boolean'
          'Strict::Bool'
        else
          raise "#{schema_doc.type} not supported"
        end
      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

11 entries across 11 versions & 1 rubygems

Version Path
cocina-models-0.39.0 lib/cocina/generator/schema_base.rb
cocina-models-0.38.0 lib/cocina/generator/schema_base.rb
cocina-models-0.37.0 lib/cocina/generator/schema_base.rb
cocina-models-0.36.0 lib/cocina/generator/schema_base.rb
cocina-models-0.35.0 lib/cocina/generator/schema_base.rb
cocina-models-0.34.1 lib/cocina/generator/schema_base.rb
cocina-models-0.34.0 lib/cocina/generator/schema_base.rb
cocina-models-0.33.0 lib/cocina/generator/schema_base.rb
cocina-models-0.32.0 lib/cocina/generator/schema_base.rb
cocina-models-0.31.1 lib/cocina/generator/schema_base.rb
cocina-models-0.31.0 lib/cocina/generator/schema_base.rb