Sha256: e31ef2e41aeb0fdadde74d6262266781bc9f392855f203c757e60d05f535a29f

Contents?: true

Size: 1.32 KB

Versions: 23

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Cocina
  module Generator
    # Class for generating from an openapi value
    class SchemaValue < SchemaBase
      def generate
        if required && !relaxed
          "#{preamble}attribute :#{name.camelize(:lower)}, #{type}"
        else
          "#{preamble}attribute? :#{name.camelize(:lower)}, #{type}"
        end
      end

      private

      def type
        # optional has to come before default or the default value that gets set will be nil.
        "#{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(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

23 entries across 23 versions & 1 rubygems

Version Path
cocina-models-0.99.1 lib/cocina/generator/schema_value.rb
cocina-models-0.99.0 lib/cocina/generator/schema_value.rb
cocina-models-0.98.1 lib/cocina/generator/schema_value.rb
cocina-models-0.98.0 lib/cocina/generator/schema_value.rb
cocina-models-0.96.0 lib/cocina/generator/schema_value.rb
cocina-models-0.95.1 lib/cocina/generator/schema_value.rb
cocina-models-0.95.0 lib/cocina/generator/schema_value.rb
cocina-models-0.94.2 lib/cocina/generator/schema_value.rb
cocina-models-0.94.1 lib/cocina/generator/schema_value.rb
cocina-models-0.94.0 lib/cocina/generator/schema_value.rb
cocina-models-0.93.1 lib/cocina/generator/schema_value.rb
cocina-models-0.93.0 lib/cocina/generator/schema_value.rb
cocina-models-0.92.0 lib/cocina/generator/schema_value.rb
cocina-models-0.91.4 lib/cocina/generator/schema_value.rb
cocina-models-0.91.3 lib/cocina/generator/schema_value.rb
cocina-models-0.91.2 lib/cocina/generator/schema_value.rb
cocina-models-0.91.1 lib/cocina/generator/schema_value.rb
cocina-models-0.91.0 lib/cocina/generator/schema_value.rb
cocina-models-0.90.0 lib/cocina/generator/schema_value.rb
cocina-models-0.89.1 lib/cocina/generator/schema_value.rb