lib/cocina/generator/schema_value.rb in cocina-models-0.75.0 vs lib/cocina/generator/schema_value.rb in cocina-models-0.76.0
- old
+ new
@@ -2,19 +2,29 @@
module Cocina
module Generator
# Class for generating from an openapi value
class SchemaValue < SchemaBase
- # rubocop:disable Layout/LineLength
def generate
# optional has to come before default or the default value that gets set will be nil.
- "#{description}#{example}#{relaxed_comment}attribute :#{name.camelize(:lower)}, Types::#{dry_datatype(schema_doc)}#{optional}#{default}#{enum}#{omittable}"
+ if required && !relaxed
+ "#{preamble}attribute :#{name.camelize(:lower)}, #{type}"
+ else
+ "#{preamble}attribute? :#{name.camelize(:lower)}, #{type}"
+ end
end
- # rubocop:enable Layout/LineLength
private
+ def type
+ "Types::#{dry_datatype(schema_doc)}#{optional}#{default}#{enum}"
+ end
+
+ def preamble
+ "#{description}#{example}#{relaxed_comment}"
+ end
+
def enum
return '' if !schema_doc.enum || relaxed
items = use_types? ? "*#{parent.name}::TYPES" : schema_doc.enum.map { |item| quote(item) }.join(', ')
@@ -30,10 +40,10 @@
return '.default(Cocina::Models::VERSION)' if name == 'cocinaVersion'
# If type is boolean and default is false, erroneously getting a nil.
# Assuming that if required, then default is false.
default = schema_doc.default
- default = false if default.nil? && schema_doc.type == 'boolean' && required
+ default = false if default.nil? && schema_doc.type == 'boolean'
return '' if default.nil?
".default(#{quote(default)})"
end