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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SchemaBase.



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

def initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil, relaxed: false)
  @schema_doc = schema_doc
  @key = key
  @required = required
  @nullable = nullable
  @parent = parent
  @relaxed = relaxed
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

#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

Instance Method Details

#any_datatype?(doc) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cocina/generator/schema_base.rb', line 81

def any_datatype?(doc)
  relaxed || doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cocina/generator/schema_base.rb', line 64

def dry_datatype(doc)
  case doc.type
  when 'integer'
    'Strict::Integer'
  when 'string'
    string_dry_datatype(doc)
  when 'boolean'
    'Strict::Bool'
  else
    if any_datatype?(doc)
      'Nominal::Any'
    else
      raise "#{schema_doc.type} not supported"
    end
  end
end

#exampleObject



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

def example
  return '' unless schema_doc.example

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

#filenameObject



18
19
20
# File 'lib/cocina/generator/schema_base.rb', line 18

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

#nameObject



22
23
24
# File 'lib/cocina/generator/schema_base.rb', line 22

def name
  key || schema_doc.name
end

#omittableObject

Allows non-required values to not be provided. This allows smaller requests as not every field needs to be present.



28
29
30
31
32
# File 'lib/cocina/generator/schema_base.rb', line 28

def omittable
  return '' if required && !relaxed

  '.meta(omittable: true)'
end

#optionalObject

Allows nillable values to be set to nil. This is useful when doing an update and you want to clear out a value.



36
37
38
# File 'lib/cocina/generator/schema_base.rb', line 36

def optional
  nullable || relaxed ? '.optional' : ''
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



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

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



85
86
87
88
89
90
91
92
# File 'lib/cocina/generator/schema_base.rb', line 85

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