Class: OpenapiBlocks::Spec::Components

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_blocks/spec/components.rb

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

INPUT_IGNORED_PROPERTIES =
%w[id created_at updated_at deleted_at].freeze

Instance Method Summary collapse

Constructor Details

#initialize(openapi_classes) ⇒ Components

Returns a new instance of Components.



11
12
13
# File 'lib/openapi_blocks/spec/components.rb', line 11

def initialize(openapi_classes)
  @openapi_classes = openapi_classes
end

Instance Method Details

#buildObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openapi_blocks/spec/components.rb', line 15

def build # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  schemas = @openapi_classes.each_with_object({}) do |klass, hash|
    schema_klass = klass.respond_to?(:_resource) && klass._resource ? klass._resource : klass

    begin
      next unless schema_klass.model
    rescue StandardError
      next
    end

    schema_name = schema_klass.model.name
    extractor   = Schema::Extractor.new(schema_klass)
    validator   = Schema::Validator.new(schema_klass.model)

    schema              = extractor.extract
    schema[:properties] = merge_validations(schema[:properties], validator.extract)

    hash[schema_name]           = schema
    hash["#{schema_name}Input"] = build_input(schema, schema_klass)
  end

  { schemas: schemas }
end