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



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



116
117
118
# File 'lib/cocina/generator/schema_base.rb', line 116

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



79
80
81
# File 'lib/cocina/generator/schema_base.rb', line 79

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

#datatype_from_doc_names(doc) ⇒ Object



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

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

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

#datatype_from_doc_type(doc) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cocina/generator/schema_base.rb', line 91

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



112
113
114
# File 'lib/cocina/generator/schema_base.rb', line 112

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

#deprecationObject



60
61
62
63
64
# File 'lib/cocina/generator/schema_base.rb', line 60

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

  to_multiline_comment(schema_doc.description)
end

#dry_datatype(doc) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/cocina/generator/schema_base.rb', line 83

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



66
67
68
69
70
# File 'lib/cocina/generator/schema_base.rb', line 66

def example
  return '' unless schema_doc.example

  to_multiline_comment("example: #{schema_doc.example}")
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' if 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



132
133
134
# File 'lib/cocina/generator/schema_base.rb', line 132

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



72
73
74
75
76
# File 'lib/cocina/generator/schema_base.rb', line 72

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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cocina/generator/schema_base.rb', line 120

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

#to_multiline_comment(string) ⇒ Object



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

def to_multiline_comment(string)
  string
    .split(/(.{80,}?) /)
    .compact_blank
    .map { |comment_chunk| "# #{comment_chunk}\n" }
    .join
end