Class: Suma::SchemaCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/schema_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path_schemas: nil, manifest: nil) ⇒ SchemaCollection

Returns a new instance of SchemaCollection.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/suma/schema_collection.rb', line 15

def initialize(config: nil, config_yaml: nil, output_path_docs: nil,
               output_path_schemas: nil, manifest: nil)
  @schemas = {}
  @docs = {}
  @schema_name_to_docs = {}
  @output_path_docs = Pathname.new(output_path_docs || Dir.pwd).expand_path
  @output_path_schemas = Pathname.new(
    output_path_schemas || Dir.pwd,
  ).expand_path
  @config = config
  @config ||= config_yaml && Expressir::SchemaManifest.from_file(config_yaml)
  @manifest = manifest
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def config
  @config
end

#docsObject

Returns the value of attribute docs.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def docs
  @docs
end

#manifestObject

Returns the value of attribute manifest.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def manifest
  @manifest
end

#output_path_docsObject

Returns the value of attribute output_path_docs.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def output_path_docs
  @output_path_docs
end

#output_path_schemasObject

Returns the value of attribute output_path_schemas.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def output_path_schemas
  @output_path_schemas
end

#schemasObject

Returns the value of attribute schemas.



12
13
14
# File 'lib/suma/schema_collection.rb', line 12

def schemas
  @schemas
end

Instance Method Details

#compileObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/suma/schema_collection.rb', line 66

def compile
  finalize

  exporter = SchemaExporter.new(
    schemas: @config.schemas,
    output_path: @output_path_schemas,
    options: { annotations: false },
  )
  exporter.export

  docs.each_pair do |_schema_id, entry|
    entry.compile
  end
end

#doc_from_schema_name(schema_name) ⇒ Object



29
30
31
# File 'lib/suma/schema_collection.rb', line 29

def doc_from_schema_name(schema_name)
  @schema_name_to_docs[schema_name]
end

#finalizeObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/suma/schema_collection.rb', line 54

def finalize
  process_schemas(@config.schemas, SchemaAttachment)

  manifest_entry = @manifest.lookup_schemas_only

  manifest_entry.each do |entry|
    next unless entry.schema_config

    process_schemas(entry.schema_config.schemas, SchemaDocument)
  end
end

#process_schema(config_schema, klass) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/suma/schema_collection.rb', line 39

def process_schema(config_schema, klass)
  s = ExpressSchema.new(
    id: config_schema.id, path: config_schema.path.to_s,
    output_path: @output_path_schemas.to_s
  )

  doc = klass.new(
    schema: s, output_path: @output_path_docs.join(s.id),
  )

  @docs[s.id] = doc
  @schemas[s.id] = s
  @schema_name_to_docs[s.id] = doc
end

#process_schemas(schemas, klass) ⇒ Object



33
34
35
36
37
# File 'lib/suma/schema_collection.rb', line 33

def process_schemas(schemas, klass)
  schemas.each do |config_schema|
    process_schema(config_schema, klass)
  end
end