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, parent: nil) ⇒ SchemaBase

Returns a new instance of SchemaBase.



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

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

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
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

#descriptionObject



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

def description
  return '' unless schema_doc.description

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

#dry_datatype(doc) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cocina/generator/schema_base.rb', line 48

def dry_datatype(doc)
  case doc.type
  when 'integer'
    'Strict::Integer'
  when 'string'
    string_dry_datatype(doc)
  when 'boolean'
    'Strict::Bool'
  else
    if doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
      'Nominal::Any'
    else
      raise "#{schema_doc.type} not supported"
    end
  end
end

#exampleObject



42
43
44
45
46
# File 'lib/cocina/generator/schema_base.rb', line 42

def example
  return '' unless schema_doc.example

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

#filenameObject



16
17
18
# File 'lib/cocina/generator/schema_base.rb', line 16

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

#nameObject



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

def name
  key || schema_doc.name
end

#omittableObject



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

def omittable
  return '' if required

  '.meta(omittable: true)'
end

#quote(item) ⇒ Object



30
31
32
33
34
# File 'lib/cocina/generator/schema_base.rb', line 30

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

  "'#{item}'"
end

#string_dry_datatype(doc) ⇒ Object



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

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