Class: OpenapiRuby::Generator::SchemaWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/generator/schema_writer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_name, schema_config) ⇒ SchemaWriter

Returns a new instance of SchemaWriter.



19
20
21
22
# File 'lib/openapi_ruby/generator/schema_writer.rb', line 19

def initialize(schema_name, schema_config)
  @schema_name = schema_name
  @schema_config = schema_config
end

Class Method Details

.generate_all!Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/openapi_ruby/generator/schema_writer.rb', line 6

def self.generate_all!
  config = OpenapiRuby.configuration

  if config.schemas.empty?
    warn "[openapi_ruby] No schemas configured, skipping generation"
    return
  end

  config.schemas.each do |name, schema_config|
    new(name, schema_config).write!
  end
end

Instance Method Details

#build_documentObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/openapi_ruby/generator/schema_writer.rb', line 32

def build_document
  builder = Core::DocumentBuilder.new(@schema_config)

  # Merge paths from DSL metadata
  DSL::MetadataStore.contexts_for(@schema_name).each do |context|
    builder.add_path(context.path_template, context.to_openapi)
  end

  # Merge components from registry
  scope = @schema_config[:component_scope]
  components = Components::Registry.instance.to_openapi_hash(scope: scope)
  builder.merge_components(components)

  builder.build
end

#write!Object



24
25
26
27
28
29
30
# File 'lib/openapi_ruby/generator/schema_writer.rb', line 24

def write!
  document = build_document
  output_path = File.join(output_dir, filename)
  FileUtils.mkdir_p(output_dir)
  File.write(output_path, format_output(document))
  output_path
end