Class: Cocina::Generator::SchemaBase
- Inherits:
-
Object
- Object
- Cocina::Generator::SchemaBase
show all
- Defined in:
- lib/cocina/generator/schema_base.rb
Overview
Base class for generating from openapi
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
#key ⇒ Object
Returns the value of attribute key.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def key
@key
end
|
#nullable ⇒ Object
Returns the value of attribute nullable.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def nullable
@nullable
end
|
#parent ⇒ Object
Returns the value of attribute parent.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def parent
@parent
end
|
#relaxed ⇒ Object
Returns the value of attribute relaxed.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def relaxed
@relaxed
end
|
#required ⇒ Object
Returns the value of attribute required.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def required
@required
end
|
#schema_doc ⇒ Object
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
79
80
81
|
# File 'lib/cocina/generator/schema_base.rb', line 79
def any_datatype?(doc)
relaxed || doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
end
|
#deprecation ⇒ Object
44
45
46
47
48
|
# File 'lib/cocina/generator/schema_base.rb', line 44
def deprecation
return '' unless schema_doc.deprecated?
"# DEPRECATED\n"
end
|
#description ⇒ Object
38
39
40
41
42
|
# File 'lib/cocina/generator/schema_base.rb', line 38
def description
return '' unless schema_doc.description
"# #{schema_doc.description}\n"
end
|
#dry_datatype(doc) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/cocina/generator/schema_base.rb', line 62
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
|
#example ⇒ Object
50
51
52
53
54
|
# File 'lib/cocina/generator/schema_base.rb', line 50
def example
return '' unless schema_doc.example
"# example: #{schema_doc.example}\n"
end
|
#filename ⇒ Object
18
19
20
|
# File 'lib/cocina/generator/schema_base.rb', line 18
def filename
"#{name.underscore}.rb"
end
|
#name ⇒ Object
22
23
24
|
# File 'lib/cocina/generator/schema_base.rb', line 22
def name
key || schema_doc.name
end
|
#optional ⇒ Object
Allows nillable values to be set to nil. This is useful when doing an update and you want to clear out a value.
28
29
30
|
# File 'lib/cocina/generator/schema_base.rb', line 28
def optional
nullable || relaxed ? '.optional' : ''
end
|
#preamble ⇒ Object
92
93
94
|
# File 'lib/cocina/generator/schema_base.rb', line 92
def preamble
"#{deprecation}#{description}#{example}#{}"
end
|
#quote(item) ⇒ Object
32
33
34
35
36
|
# File 'lib/cocina/generator/schema_base.rb', line 32
def quote(item)
return item unless schema_doc.type == 'string'
"'#{item}'"
end
|
56
57
58
59
60
|
# File 'lib/cocina/generator/schema_base.rb', line 56
def
return '' unless relaxed
"# Validation of this property is relaxed. See the openapi for full validation.\n"
end
|
#string_dry_datatype(doc) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/cocina/generator/schema_base.rb', line 83
def string_dry_datatype(doc)
case doc.format
when 'date-time'
'Params::DateTime'
else
'Strict::String'
end
end
|