Class: Cocina::Generator::SchemaBase

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina/generator/schema_base.rb

Overview

Base class for generating from openapi

Direct Known Subclasses

Datatype, Schema, SchemaArray, SchemaRef, SchemaValue, UnionType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil, relaxed: false, schemas: [], lite: false) ⇒ SchemaBase

Returns a new instance of SchemaBase.



9
10
11
12
13
14
15
16
17
18
# File 'lib/cocina/generator/schema_base.rb', line 9

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def key
  @key
end

#liteObject (readonly)

Returns the value of attribute lite.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def lite
  @lite
end

#nullableObject (readonly)

Returns the value of attribute nullable.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def nullable
  @nullable
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def parent
  @parent
end

#relaxedObject (readonly)

Returns the value of attribute relaxed.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def relaxed
  @relaxed
end

#requiredObject (readonly)

Returns the value of attribute required.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def required
  @required
end

#schema_docObject (readonly)

Returns the value of attribute schema_doc.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def schema_doc
  @schema_doc
end

#schemasObject (readonly)

Returns the value of attribute schemas.



7
8
9
# File 'lib/cocina/generator/schema_base.rb', line 7

def schemas
  @schemas
end

Instance Method Details

#any_datatype?(doc) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/cocina/generator/schema_base.rb', line 108

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

#custom_type?Boolean

dry-types-based types contain the word ‘Types` (e.g., `Types::String`), and custom types (e.g., `SourceId`) do not

Returns:

  • (Boolean)


71
72
73
# File 'lib/cocina/generator/schema_base.rb', line 71

def custom_type?
  !dry_datatype(schema_doc).match?('Types')
end

#datatype_from_doc_names(doc) ⇒ Object



98
99
100
101
102
# File 'lib/cocina/generator/schema_base.rb', line 98

def datatype_from_doc_names(doc)
  return unless defined_datatypes?(doc)

  doc.one_of.map(&:name).join(' | ')
end

#datatype_from_doc_type(doc) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cocina/generator/schema_base.rb', line 83

def datatype_from_doc_type(doc)
  case doc.type
  when 'integer'
    'Types::Strict::Integer'
  when 'string'
    string_dry_datatype(doc)
  when 'boolean'
    'Types::Strict::Bool'
  else
    return 'Types::Nominal::Any' if any_datatype?(doc)

    raise "#{schema_doc.type} not supported"
  end
end

#defined_datatypes?(doc) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/cocina/generator/schema_base.rb', line 104

def defined_datatypes?(doc)
  doc.one_of&.map(&:name)&.all? { |name| name.present? && schemas.include?(name) }
end

#deprecationObject



52
53
54
55
56
# File 'lib/cocina/generator/schema_base.rb', line 52

def deprecation
  return '' unless schema_doc.deprecated?

  "# DEPRECATED\n"
end

#descriptionObject



46
47
48
49
50
# File 'lib/cocina/generator/schema_base.rb', line 46

def description
  return '' unless schema_doc.description

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

#dry_datatype(doc) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/cocina/generator/schema_base.rb', line 75

def dry_datatype(doc)
  return doc.name if doc.name.present? && schemas.include?(doc.name)

  datatype_from_doc_names(doc) ||
    datatype_from_doc_type(doc) ||
    raise("#{doc.type} not supported")
end

#exampleObject



58
59
60
61
62
# File 'lib/cocina/generator/schema_base.rb', line 58

def example
  return '' unless schema_doc.example

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

#filenameObject



20
21
22
# File 'lib/cocina/generator/schema_base.rb', line 20

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

#nameObject



24
25
26
# File 'lib/cocina/generator/schema_base.rb', line 24

def name
  "#{key || schema_doc.name}#{lite ? 'Lite' : ''}"
end

#optionalObject

Allows nullable values to be set to nil. This is useful when doing an update and you want to clear out a value. The logic also permits custom types (e.g., ‘Barcode`, `SourceId`) to be nullable if they are not required.



32
33
34
35
36
37
38
# File 'lib/cocina/generator/schema_base.rb', line 32

def optional
  return '.optional' if nullable ||
                        relaxed ||
                        (custom_type? && !required)

  ''
end

#preambleObject



124
125
126
# File 'lib/cocina/generator/schema_base.rb', line 124

def preamble
  "#{deprecation}#{description}#{example}#{relaxed_comment}"
end

#quote(item) ⇒ Object



40
41
42
43
44
# File 'lib/cocina/generator/schema_base.rb', line 40

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

  "'#{item}'"
end

#relaxed_commentObject



64
65
66
67
68
# File 'lib/cocina/generator/schema_base.rb', line 64

def relaxed_comment
  return '' unless relaxed

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

#string_dry_datatype(doc) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cocina/generator/schema_base.rb', line 112

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

  "Types::Strict::String.constrained(format: /#{doc.pattern}/)"
end