Sha256: b282761a733d850145e6e6cf2ac0e18ae4a73e91bb6c357355e6f191b6bf9df4

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require_relative 'ref_schema_builder'
require_relative 'dynamic_schema_builder'
require_relative 'array_schema_builder'
require_relative 'object_schema_builder'

module Meta
  module JsonSchema
    class SchemaBuilderTool
      class << self
        SCHEMA_BUILDER_OPTIONS = Utils::KeywordArgs::Builder.build do
          permit_extras true

          key :ref, alias_names: [:using]
          key :dynamic_ref, alias_names: [:dynamic_using], normalizer: ->(value) { value.is_a?(Proc) ? { resolve: value } : value }
        end
        def build(options = {}, &block)
          options = SCHEMA_BUILDER_OPTIONS.check(options)

          if apply_array_schema?(options, block)
            ArraySchemaBuilder.new(options, &block).to_schema
          elsif apply_ref_schema?(options, block)
            RefSchemaBuilder.new(options).to_schema
          elsif apply_dynamic_schema?(options, block)
            DynamicSchemaBuilder.new(options).to_schema
          elsif apply_object_schema?(options, block)
            ObjectSchemaBuilder.new(options, &block).to_schema
          else
            BaseSchema.new(options)
          end
        end

        private

        def apply_array_schema?(options, block)
          options[:type] == 'array' && (options[:items] || options[:ref] || options[:dynamic_ref] || block)
        end

        def apply_object_schema?(options, block)
          (options[:type] == 'object' || options[:type].nil?) && (options[:properties] || block)
        end

        def apply_ref_schema?(options, block)
          options[:ref] != nil
        end

        def apply_dynamic_schema?(options, block)
          options[:dynamic_ref] != nil
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
meta-api-0.0.5 lib/meta/json_schema/builders/schema_builder_tool.rb
meta-api-0.0.4 lib/meta/json_schema/builders/schema_builder_tool.rb
meta-api-0.0.3 lib/meta/json_schema/builders/schema_builder_tool.rb