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
VALID_OPTIONS =
%i[title description optional as validate ref].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:

  • name (Symbol)

    The name of the composition.

  • type (Class)

    The type of the composition.

  • constraints (Hash)

    The constraints for the composition.



28
29
30
31
32
33
34
35
# File 'lib/easy_talk/builders/composition_builder.rb', line 28

def initialize(name, type, constraints)
  EasyTalk.assert_valid_property_options(name, constraints, VALID_OPTIONS)
  @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:

  • (Boolean)

Instance Method Details

#buildObject



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

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

#composer_keywordObject



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

def composer_keyword
  COMPOSER_TO_KEYWORD[@composer_type]
end

#itemsObject



77
78
79
# File 'lib/easy_talk/builders/composition_builder.rb', line 77

def items
  @type.items
end

#schemasObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/easy_talk/builders/composition_builder.rb', line 60

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