class Argo::PropertyFactory

Constants

TYPE_MAP

Public Class Methods

new(dereferencer, required_fields = []) click to toggle source
# File lib/argo/property_factory.rb, line 25
def initialize(dereferencer, required_fields = [])
  @dereferencer = dereferencer
  @required_fields = required_fields
end

Public Instance Methods

build(body, name: nil) click to toggle source
# File lib/argo/property_factory.rb, line 32
def build(body, name: nil)
  class_for_type(body).new(
    constraints: constraints(body),
    description: body['description'],
    required: required?(name),
    **additional_properties(body)
  )
end

Private Instance Methods

additional_properties(body) click to toggle source
# File lib/argo/property_factory.rb, line 64
def additional_properties(body)
  if class_for_type(body) == ArrayProperty
    additional_properties_for_array(body)
  else
    {}
  end
end
additional_properties_for_array(body) click to toggle source
# File lib/argo/property_factory.rb, line 72
def additional_properties_for_array(body)
  items = [body.fetch('items')].flatten.first
  if reference?(items)
    value = dereference(items)
  else
    factory = PropertyFactory.new(@dereferencer)
    value = factory.build(items)
  end
  { items: value }
end
class_for_type(body) click to toggle source
# File lib/argo/property_factory.rb, line 43
def class_for_type(body)
  klass = explicit_class(body) || implicit_class(body)
  raise "No type found in #{body.inspect}" unless klass
  klass
end
constraints(hash) click to toggle source
# File lib/argo/property_factory.rb, line 87
def constraints(hash)
  ConstraintProcessor.new(@dereferencer).process(hash)
end
explicit_class(body) click to toggle source
# File lib/argo/property_factory.rb, line 49
def explicit_class(body)
  return nil unless body.key?('type')
  type = body.fetch('type')
  raise "Unknown property type '#{type}'" unless TYPE_MAP.key?(type)
  TYPE_MAP.fetch(type)
end
implicit_class(body) click to toggle source
# File lib/argo/property_factory.rb, line 56
def implicit_class(body)
  if body.key?('enum')
    StringProperty
  elsif (body.keys & %w[ oneOf anyOf ]).any?
    ObjectProperty
  end
end
required?(name) click to toggle source
# File lib/argo/property_factory.rb, line 83
def required?(name)
  @required_fields.include?(name)
end