Sha256: 6ffe3cc4223fa0573ba5a9400be56c98587bff744b9afa20ccc74627ee833cf1

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require_relative '../../utils/kwargs/builder'

module Meta
  module JsonSchema
    module SchemaOptions
      @default_options = {
        scope: [],
        required: false
      }

      class << self
        def divide_to_param_and_render(options)
          common_opts = (options || {}).dup
          param_opts = common_opts.delete(:param)
          render_opts = common_opts.delete(:render)

          param_opts = merge_common_to_stage(common_opts, param_opts)
          render_opts = merge_common_to_stage(common_opts, render_opts)
          [param_opts, render_opts, common_opts]
        end

        def normalize(options)
          # 只要 options 中设置为 nil 的选项没有明确的意义,则下行代码是永远有效的
          options = (@default_options.compact).merge(options.compact)
          if options[:using]
            if options[:type].nil?
              options[:type] = 'object'
            elsif options[:type] != 'object' && options[:type] != 'array'
              raise "当使用 using 时,type 必须声明为 object 或 array"
            end
          end

          options
        end

        private

        def merge_common_to_stage(common_opts, stage_opts)
          stage_opts = {} if stage_opts.nil? || stage_opts == true
          stage_opts = common_opts.merge(stage_opts) if stage_opts
          stage_opts
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
meta-api-0.1.2 lib//meta/json_schema/support/schema_options.rb
meta-api-0.1.1 lib//meta/json_schema/support/schema_options.rb
meta-api-0.1.0 lib//meta/json_schema/support/schema_options.rb
meta-api-0.0.9 lib//meta/json_schema/support/schema_options.rb