Class: Esquema::SchemaEnhancer

Inherits:
Object
  • Object
show all
Defined in:
lib/esquema/schema_enhancer.rb

Constant Summary collapse

VALID_OPTIONS =
i[title description maxLength minLength pattern maxItems minItems uniqueItems
maxProperties minProperties properties additionalProperties dependencies
enum format multipleOf maximum exclusiveMaximum minimum exclusiveMinimum
const allOf anyOf oneOf not default].freeze
TYPE_MAPPINGS =
{
  date: Date,
  datetime: DateTime,
  time: Time,
  string: String,
  text: String,
  integer: Integer,
  float: Float,
  decimal: BigDecimal,
  boolean: [TrueClass, FalseClass],
  array: Array,
  object: Object
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, schema_enhancements) ⇒ SchemaEnhancer

Returns a new instance of SchemaEnhancer.



26
27
28
29
# File 'lib/esquema/schema_enhancer.rb', line 26

def initialize(model, schema_enhancements)
  @schema_enhancements = schema_enhancements
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



24
25
26
# File 'lib/esquema/schema_enhancer.rb', line 24

def model
  @model
end

Instance Method Details

#model_description(description) ⇒ Object



31
32
33
# File 'lib/esquema/schema_enhancer.rb', line 31

def model_description(description)
  @schema_enhancements[:model_description] = description
end

#model_title(title) ⇒ Object



35
36
37
# File 'lib/esquema/schema_enhancer.rb', line 35

def model_title(title)
  @schema_enhancements[:model_title] = title
end

#property(name, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/esquema/schema_enhancer.rb', line 39

def property(name, options = {})
  db_type = model.type_for_attribute(name).type
  klass_type = Array(TYPE_MAPPINGS[db_type])

  validate_default_value(options[:default], klass_type, db_type)
  validate_enum_values(options[:enum], klass_type, db_type)

  options.assert_valid_keys(VALID_OPTIONS)
  @schema_enhancements[:properties] ||= {}
  @schema_enhancements[:properties][name] = options
end