Sha256: 11b6602749dc3f883e2c19408f748aba060d06cfc39c15b2a495345ec760d93c

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Cocina
  module Generator
    # Class for generating from an openapi value
    class SchemaValue < SchemaBase
      # rubocop:disable Layout/LineLength
      def generate
        # optional has to come before default or the default value that gets set will be nil.
        "#{description}#{example}#{relaxed_comment}attribute :#{name.camelize(:lower)}, Types::#{dry_datatype(schema_doc)}#{optional}#{default}#{enum}#{omittable}"
      end
      # rubocop:enable Layout/LineLength

      private

      def enum
        return '' if !schema_doc.enum || relaxed

        items = use_types? ? "*#{parent.name}::TYPES" : schema_doc.enum.map { |item| quote(item) }.join(', ')

        ".enum(#{items})"
      end

      def use_types?
        parent.is_a?(Schema) && key == 'type'
      end

      def default
        # Provide version as default for cocinaVersion
        return '.default(Cocina::Models::VERSION)' if name == 'cocinaVersion'

        # If type is boolean and default is false, erroneously getting a nil.
        # Assuming that if required, then default is false.
        default = schema_doc.default
        default = false if default.nil? && schema_doc.type == 'boolean' && required

        return '' if default.nil?

        ".default(#{quote(default)})"
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
cocina-models-0.75.0 lib/cocina/generator/schema_value.rb
cocina-models-0.74.1 lib/cocina/generator/schema_value.rb
cocina-models-0.74.0 lib/cocina/generator/schema_value.rb
cocina-models-0.73.6 lib/cocina/generator/schema_value.rb
cocina-models-0.73.5 lib/cocina/generator/schema_value.rb
cocina-models-0.73.4 lib/cocina/generator/schema_value.rb
cocina-models-0.73.3 lib/cocina/generator/schema_value.rb
cocina-models-0.73.2 lib/cocina/generator/schema_value.rb
cocina-models-0.73.1 lib/cocina/generator/schema_value.rb
cocina-models-0.73.0 lib/cocina/generator/schema_value.rb
cocina-models-0.72.0 lib/cocina/generator/schema_value.rb
cocina-models-0.71.0 lib/cocina/generator/schema_value.rb
cocina-models-0.70.0 lib/cocina/generator/schema_value.rb
cocina-models-0.69.2 lib/cocina/generator/schema_value.rb
cocina-models-0.69.1 lib/cocina/generator/schema_value.rb
cocina-models-0.69.0 lib/cocina/generator/schema_value.rb
cocina-models-0.68.0 lib/cocina/generator/schema_value.rb
cocina-models-0.67.1 lib/cocina/generator/schema_value.rb
cocina-models-0.67.0 lib/cocina/generator/schema_value.rb
cocina-models-0.66.0 lib/cocina/generator/schema_value.rb