Class: EasyTalk::Builders::CompositionBuilder

Inherits:
Object
  • Object
show all
Extended by:
CollectionHelpers, T::Sig
Defined in:
lib/easy_talk/builders/composition_builder.rb

Overview

This class represents a builder for composing JSON schemas using the "allOf", "anyOf", or "oneOf" keywords.

Direct Known Subclasses

AllOfBuilder, AnyOfBuilder, OneOfBuilder

Defined Under Namespace

Classes: AllOfBuilder, AnyOfBuilder, OneOfBuilder

Constant Summary collapse

COMPOSER_TO_KEYWORD =
{
  'AllOfBuilder' => 'allOf',
  'AnyOfBuilder' => 'anyOf',
  'OneOfBuilder' => 'oneOf'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, constraints) ⇒ CompositionBuilder

Initializes a new instance of the CompositionBuilder class.

Parameters:

  • The name of the composition.

  • The type of the composition.

  • The constraints for the composition.



26
27
28
29
30
31
32
# File 'lib/easy_talk/builders/composition_builder.rb', line 26

def initialize(name, type, constraints)
  @composer_type = self.class.name.split('::').last
  @name = name
  @type = type
  @context = {}
  @constraints = constraints
end

Class Method Details

.collection_type?Boolean Originally defined in module CollectionHelpers

Returns:

Instance Method Details

#buildObject



38
39
40
41
42
43
# File 'lib/easy_talk/builders/composition_builder.rb', line 38

def build
  @context[@name.to_sym] = {
    type: 'object',
    composer_keyword => schemas
  }
end

#composer_keywordObject



49
50
51
# File 'lib/easy_talk/builders/composition_builder.rb', line 49

def composer_keyword
  COMPOSER_TO_KEYWORD[@composer_type]
end

#itemsObject



74
75
76
# File 'lib/easy_talk/builders/composition_builder.rb', line 74

def items
  @type.items
end

#schemasObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/easy_talk/builders/composition_builder.rb', line 57

def schemas
  items.map do |type|
    if EasyTalk::RefHelper.should_use_ref?(type, @constraints)
      EasyTalk::RefHelper.build_ref_schema(type, @constraints)
    elsif type.respond_to?(:schema)
      type.schema
    else
      # Map Ruby type to JSON Schema type
      { type: TypeIntrospection.json_schema_type(type) }
    end
  end
end