Sha256: f6482c58f35eebaffb7ad04bc199c8613fbe9e399c220c4c797d405820a5c80a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require_relative '../support/scope_matcher'

module Meta
  module JsonSchema
    class ScopingSchema < BaseSchema
      attr_reader :scope_matcher, :schema

      def initialize(scope_matcher_options: , schema:)
        @scope_matcher = ScopeMatcher.new(scope_matcher_options)
        @schema = schema
      end

      def scoped(user_scopes)
        @scope_matcher.match?(user_scopes) ? schema : unsupported_schema(user_scopes)
      end

      def defined_scopes(**kwargs)
        current = scope_matcher.defined_scopes
        deep = schema.defined_scopes(**kwargs)
        (current + deep).uniq
      end

      private

      def unsupported_schema(user_scopes)
        UnsupportedSchema.new(:scope, user_scopes)
      end

      def self.build_from_options(options, build_schema)
        options = options.dup
        scope_matcher_options = options.delete(:scope)
        schema = build_schema.call(options)
        schema = ScopingSchema.new(scope_matcher_options: scope_matcher_options, schema: schema) if scope_matcher_options
        schema
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
meta-api-0.1.1 lib//meta/json_schema/schemas/scoping_schema.rb