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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from CollectionHelpers

collection_type?

Constructor Details

#initialize(name, type, _constraints) ⇒ CompositionBuilder

Initializes a new instance of the CompositionBuilder class.

Parameters:

  • name (Symbol)

    The name of the composition.

  • type (Class)

    The type of the composition.

  • _constraints (Hash)

    The constraints for the composition (not used in this method).



24
25
26
27
28
29
# File 'lib/easy_talk/builders/composition_builder.rb', line 24

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

Instance Method Details

#buildvoid

This method returns an undefined value.

Builds the composed JSON schema.



34
35
36
37
38
39
# File 'lib/easy_talk/builders/composition_builder.rb', line 34

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

#composer_keywordString

Returns the composer keyword based on the composer type.

Returns:

  • (String)

    The composer keyword.



44
45
46
# File 'lib/easy_talk/builders/composition_builder.rb', line 44

def composer_keyword
  COMPOSER_TO_KEYWORD[@composer_type]
end

#itemsT.untyped

Returns the items of the type.

Returns:

  • (T.untyped)

    The items of the type.



60
61
62
# File 'lib/easy_talk/builders/composition_builder.rb', line 60

def items
  @type.items
end

#schemasArray<Hash>

Returns an array of schemas for the composed JSON schema.

Returns:

  • (Array<Hash>)

    The array of schemas.



51
52
53
54
55
# File 'lib/easy_talk/builders/composition_builder.rb', line 51

def schemas
  items.map do |type|
    type.respond_to?(:schema) ? type.schema : { type: type.to_s.downcase }
  end
end