Class: Cocina::Generator::SchemaBase
- Inherits:
-
Object
- Object
- Cocina::Generator::SchemaBase
- Defined in:
- lib/cocina/generator/schema_base.rb
Overview
Base class for generating from openapi
Direct Known Subclasses
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#relaxed ⇒ Object
readonly
Returns the value of attribute relaxed.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#schema_doc ⇒ Object
readonly
Returns the value of attribute schema_doc.
Instance Method Summary collapse
- #any_datatype?(doc) ⇒ Boolean
- #description ⇒ Object
- #dry_datatype(doc) ⇒ Object
- #example ⇒ Object
- #filename ⇒ Object
-
#initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil, relaxed: false) ⇒ SchemaBase
constructor
A new instance of SchemaBase.
- #name ⇒ Object
-
#omittable ⇒ Object
Allows non-required values to not be provided.
-
#optional ⇒ Object
Allows nillable values to be set to nil.
- #quote(item) ⇒ Object
- #relaxed_comment ⇒ Object
- #string_dry_datatype(doc) ⇒ Object
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 (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/cocina/generator/schema_base.rb', line 7 def key @key end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
7 8 9 |
# File 'lib/cocina/generator/schema_base.rb', line 7 def nullable @nullable end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/cocina/generator/schema_base.rb', line 7 def parent @parent end |
#relaxed ⇒ Object (readonly)
Returns the value of attribute relaxed.
7 8 9 |
# File 'lib/cocina/generator/schema_base.rb', line 7 def relaxed @relaxed end |
#required ⇒ Object (readonly)
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 (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
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 |
#description ⇒ Object
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 |
#example ⇒ Object
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 |
#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 |
#omittable ⇒ Object
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 |
#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.
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_comment ⇒ Object
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 |