class Esquema::Property

rubocop:disable Metrics/ClassLength
Represents a property in the Esquema schema.

def as_json

Returns:
  • (Hash) - The JSON representation of the Property.
def as_json
  KEYWORDS.each_with_object({}) do |property, hash|
    value = send("build_#{property.downcase}")
    next if value.nil? || (value.is_a?(String) && value.empty?)
    hash[property] = value
  end.compact
end

def build_additionalproperties

def build_additionalproperties
  options[:additionalProperties]
end

def build_allof

def build_allof
  options[:allOf]
end

def build_anyof

def build_anyof
  options[:anyOf]
end

def build_const

def build_const
  options[:const]
end

def build_default

Returns:
  • (Object, nil) - The default attribute.
def build_default
  return unless object.respond_to?(:default)
  default_value = object.default || options[:default].presence
  @default = TypeCaster.cast(object.type, default_value) unless default_value.nil?
end

def build_dependencies

def build_dependencies
  options[:dependencies]
end

def build_description

Returns:
  • (String, nil) - The description attribute.
def build_description
  options[:description]
end

def build_enum

Returns:
  • (Array, nil) - The enum attribute.
def build_enum
  options[:enum]
end

def build_exclusivemaximum

def build_exclusivemaximum
  options[:exclusiveMaximum]
end

def build_exclusiveminimum

def build_exclusiveminimum
  options[:exclusiveMinimum]
end

def build_format

def build_format
  options[:format]
end

def build_items

Returns:
  • (Hash, nil) - The items attribute.
def build_items
  return unless object.try(:collection?)
  case object.type
  when :array
    { type: DB_TO_JSON_TYPE_MAPPINGS[object.item_type] }
  else
    Builder.new(object.klass).build_schema
  end
end

def build_maximum

def build_maximum
  options[:maximum]
end

def build_maxitems # rubocop:disable Metrics/AbcSize

rubocop:disable Metrics/AbcSize
def build_maxitems # rubocop:disable Metrics/AbcSize
  raise ArgumentError, "maxItems must be an integer" if options[:maxItems] && !options[:maxItems].is_a?(Integer)
  if options[:maxItems]&.negative?
    raise ArgumentError,
          "maxItems must be a non-negative integer"
  end
  if options[:maxItems] && options[:type] != :array
    raise ArgumentError, "maxItems must be use for array type properties only."
  end
  options[:maxItems]
end

def build_maxlength

def build_maxlength
  options[:maxLength]
end

def build_maxproperties

def build_maxproperties
  options[:maxProperties]
end

def build_minimum

def build_minimum
  options[:minimum]
end

def build_minitems # rubocop:disable Metrics/AbcSize

rubocop:disable Metrics/AbcSize
def build_minitems # rubocop:disable Metrics/AbcSize
  raise ArgumentError, "minItems must be an integer" if options[:minItems] && !options[:minItems].is_a?(Integer)
  if options[:minItems]&.negative?
    raise ArgumentError,
          "minItems must be a non-negative integer"
  end
  if options[:minItems] && options[:type] != :array
    raise ArgumentError, "minItems must be use for array type properties only."
  end
  options[:minItems]
end

def build_minlength

def build_minlength
  options[:minLength]
end

def build_minproperties

def build_minproperties
  options[:minProperties]
end

def build_multipleof

def build_multipleof
  options[:multipleof]
end

def build_not

def build_not
  options[:not]
end

def build_oneof

def build_oneof
  options[:oneOf]
end

def build_pattern

def build_pattern
  options[:pattern]
end

def build_properties

def build_properties
  options[:properties]
end

def build_title

Returns:
  • (String) - The title attribute.
def build_title
  options[:title].presence || object.name.to_s.humanize
end

def build_type

Returns:
  • (String, nil) - The type attribute.
def build_type
  return DB_TO_JSON_TYPE_MAPPINGS[:array] if object.try(:collection?)
  return unless object.respond_to?(:type)
  @type = DB_TO_JSON_TYPE_MAPPINGS[object.type]
end

def build_uniqueitems

def build_uniqueitems
  options[:uniqueItems]
end

def initialize(object, options = {})

Raises:
  • (ArgumentError) - If the property does not have a name.

Parameters:
  • options (Hash) -- Additional options for the property.
  • object (Object) -- The object to build the property for.
def initialize(object, options = {})
  raise ArgumentError, "property must have a name" unless object.respond_to?(:name)
  @object = object
  @options = options
end