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, 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
#key ⇒ Object
Returns the value of attribute key.
7
8
9
|
# File 'lib/cocina/generator/schema_base.rb', line 7
def key
@key
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
|
#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
#description ⇒ Object
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
|
#example ⇒ Object
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
|
#filename ⇒ Object
16
17
18
|
# File 'lib/cocina/generator/schema_base.rb', line 16
def filename
"#{name.underscore}.rb"
end
|
#name ⇒ Object
20
21
22
|
# File 'lib/cocina/generator/schema_base.rb', line 20
def name
key || schema_doc.name
end
|
#omittable ⇒ Object
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
|