Module: Eazypi::Schema

Defined in:
lib/eazypi/schema.rb,
lib/eazypi/schema/array.rb,
lib/eazypi/schema/object.rb,
lib/eazypi/schema/primitive.rb

Overview

JSON schema

Defined Under Namespace

Classes: Array, Object, Primitive

Class Method Summary collapse

Class Method Details

.from_object(object, parent = nil) ⇒ Object

rubocop:todo Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eazypi/schema.rb', line 6

def self.from_object(object, parent = nil) # rubocop:todo Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
  if object == :boolean
    Schema::Primitive.new(type: "boolean")
  elsif object.is_a?(::Array)
    raise "Array needs to have one element" if object.length != 1

    Schema::Array.new(Schema.from_object(object[0], parent))
  elsif object == String
    Schema::Primitive.new(type: "string")
  elsif object == Date
    Schema::Primitive.new(type: "string", format: "date")
  elsif object == Time
    Schema::Primitive.new(type: "string", format: "date-time")
  elsif object == Integer
    Schema::Primitive.new(type: "integer")
  elsif object == Float
    Schema::Primitive.new(type: "number")
  elsif object.respond_to?(:to_schema)
    if object == parent
      object.to_schema_reference
    else
      object.to_schema
    end
  else
    raise "Can not convert #{object} to a schema"
  end
end