Sha256: 39b07f2346cbe5742e53283addd94095eb1f3c4a55d30d626f8ca0fd333bbef8

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
# typed: true

module EasyTalk
  module Builders
    # BaseBuilder is a class that provides a common structure for building schema properties
    class BaseBuilder
      extend T::Sig

      # BaseBuilder is a class that provides a common structure for building objects
      # representing schema properties.
      COMMON_OPTIONS = {
        title: { type: T.nilable(String), key: :title },
        description: { type: T.nilable(String), key: :description }
      }.freeze

      attr_reader :name, :schema, :options

      sig do
        params(
          name: Symbol,
          schema: T::Hash[Symbol, T.untyped],
          options: T::Hash[Symbol, String],
          valid_options: T::Hash[Symbol, T.untyped]
        ).void
      end
      # Initializes a new instance of the BaseBuilder class.
      #
      # @param name [Symbol] The name of the property.
      # @param schema [Hash] A hash representing a json schema object.
      # @param options [Hash] The options for the builder (default: {}).
      # @param valid_options [Hash] The acceptable options for the given property type (default: {}).
      def initialize(name, schema, options = {}, valid_options = {})
        @valid_options = COMMON_OPTIONS.merge(valid_options)
        options.assert_valid_keys(@valid_options.keys)
        @name = name
        @schema = schema
        @options = options
      end

      # Builds the schema object based on the provided options.
      sig { returns(T::Hash[Symbol, T.untyped]) }
      def build
        @valid_options.each_with_object(schema) do |(key, value), obj|
          next if @options[key].nil?

          # Work around for Sorbet's default inability to type check the items inside an array

          if value[:type].respond_to?(:recursively_valid?) && !value[:type].recursively_valid?(@options[key])
            raise TypeError, "Invalid type for #{key}"
          end

          obj[value[:key]] = T.let(@options[key], value[:type])
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
easy_talk-0.1.6 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.5 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.4 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.3 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.2 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.1 lib/easy_talk/builders/base_builder.rb
easy_talk-0.1.0 lib/easy_talk/builders/base_builder.rb