Sha256: b80127858c903ef002054cde2fd78bf11fa0d9633981823dbeb2a055c881b194

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Cocina
  module Generator
    # Class for generating from an openapi value
    class SchemaValue < SchemaBase
      def generate
        # optional has to come before default or the default value that gets set will be nil.
        if required && !relaxed
          "#{preamble}attribute :#{name.camelize(:lower)}, #{type}"
        else
          "#{preamble}attribute? :#{name.camelize(:lower)}, #{type}"
        end
      end

      private

      def type
        "Types::#{dry_datatype(schema_doc)}#{optional}#{default}#{enum}"
      end

      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'

        return '' if default.nil?

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
cocina-models-0.86.0 lib/cocina/generator/schema_value.rb
cocina-models-0.85.0 lib/cocina/generator/schema_value.rb
cocina-models-0.84.5 lib/cocina/generator/schema_value.rb
cocina-models-0.84.4 lib/cocina/generator/schema_value.rb
cocina-models-0.84.3 lib/cocina/generator/schema_value.rb
cocina-models-0.84.2 lib/cocina/generator/schema_value.rb
cocina-models-0.84.1 lib/cocina/generator/schema_value.rb
cocina-models-0.84.0 lib/cocina/generator/schema_value.rb
cocina-models-0.83.0 lib/cocina/generator/schema_value.rb
cocina-models-0.82.0 lib/cocina/generator/schema_value.rb
cocina-models-0.81.0 lib/cocina/generator/schema_value.rb
cocina-models-0.80.0 lib/cocina/generator/schema_value.rb