Class: Ea::Export::JsonSchema::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/export/json_schema/generator.rb

Overview

Generates a JSON Schema (draft 2020-12) document from a parsed QEA model. Each UML Class becomes a $defs entry with properties derived from its attributes.

Constant Summary collapse

SCHEMA_URI =
"https://json-schema.org/draft/2020-12/schema".freeze
TYPE_MAP =

EA primitive → JSON Schema type

{
  "int" => "integer", "Integer" => "integer",
  "long" => "integer", "Long" => "integer",
  "double" => "number", "Double" => "number",
  "float" => "number", "Float" => "number",
  "boolean" => "boolean", "Boolean" => "boolean",
  "String" => "string", "string" => "string",
  "char" => "string", "Character" => "string",
  "Date" => "string", "DateTime" => "string"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Generator

Returns a new instance of Generator.



32
33
34
# File 'lib/ea/export/json_schema/generator.rb', line 32

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



30
31
32
# File 'lib/ea/export/json_schema/generator.rb', line 30

def model
  @model
end

Class Method Details

.call(model, **_opts) ⇒ Object



26
27
28
# File 'lib/ea/export/json_schema/generator.rb', line 26

def self.call(model, **_opts)
  new(model).call
end

Instance Method Details

#callObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ea/export/json_schema/generator.rb', line 36

def call
  classes = (model.collections[:objects] || [])
             .select { |o| o.object_type == "Class" }
  defs = classes.each_with_object({}) do |klass, acc|
    acc[klass.name] = class_schema(klass)
  end

  {
    "$schema" => SCHEMA_URI,
    "$id" => "https://example.com/schema.json",
    "type" => "object",
    "$defs" => defs
  }.to_json
end