class Json::Schema::Subset::DSL
Constants
- VERSION
Public Class Methods
new(type: "object", schema: {}, params: {}, ref: nil, options: nil, &block)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 7 def initialize(type: "object", schema: {}, params: {}, ref: nil, options: nil, &block) @type = type @schema = schema @params = params @ref = ref @options = options || {} @optionals = [] set!(&block) if block_given? end
Public Instance Methods
array!(&block)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 69 def array!(&block) change_type!("array") @schema["items"] = DSL.new(options: @options, &block) end
canon_name!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 126 def canon_name!(name) @options[:reference_name] ? @options[:reference_name].call(name.to_s) : name.to_s end
change_type!(type)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 113 def change_type!(type) case @type when "object" if type == "null" @type = %w[object null] else @type = type end else @type = Array(@type) + [type] end end
compile!()
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 21 def compile! return { "$ref" => @ref } if @ref case when Array(@type).include?("array") ret = @schema.dup ret["type"] = @type ret["items"] = ret["items"].compile! ret.merge(@params.map { |k, v| [k.to_s, v] }.to_h) when Array(@type).include?("object") required = @schema.keys - @optionals ret = { "type" => @type, "properties" => @schema.map { |k, v| [k, v.compile!] }.to_h } ret["required"] = required unless required.empty? ret.merge(@params.map { |k, v| [k.to_s, v] }.to_h) else @schema.merge("type" => @type).merge(@params).map { |k, v| [k.to_s, v] }.to_h end end
components!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 61 def components!(name) "#/components/#{canon_name!(name)}" end
cref!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 53 def cref!(name) ref! components!(name) end
definitions!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 65 def definitions!(name) "#/definitions/#{canon_name!(name)}" end
dref!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 57 def dref!(name) ref! definitions!(name) end
method_missing(name, *args, &block)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 78 def method_missing(name, *args, &block) if name.to_s.end_with?("!") if block_given? @params[name[0...-1]] = DSL.new(options: @options, &block).compile! else @params[name[0...-1]] = args[0] end return end type = args.first opts = args.count > 1 && args.last.is_a?(Hash) ? args.last || {} : {} optional = opts.delete(:optional) @optionals << name.to_s if optional type = type.is_a?(Array) ? type.map(&:to_s) : type.to_s case when type == "ref" @schema[name.to_s] = DSL.new(ref: args[1], options: @options, &block) when type == "cref" @schema[name.to_s] = DSL.new(ref: components!(args[1]), options: @options, &block) when type == "dref" @schema[name.to_s] = DSL.new(ref: definitions!(args[1]), options: @options, &block) when Array(type).include?("array") @schema[name.to_s] = DSL.new( type: type, params: opts, schema: { "items" => DSL.new(options: @options, &block) }, options: @options, ) when Array(type).include?("object") @schema[name.to_s] = DSL.new(type: type, params: opts, options: @options, &block) else @schema[name.to_s] = DSL.new(type: type, schema: opts, options: @options, &block) end end
ref!(name)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 49 def ref!(name) @ref = name.to_s end
respond_to_missing?(name, include_private)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 74 def respond_to_missing?(name, include_private) true end
set!(&block)
click to toggle source
# File lib/json/schema/subset/dsl.rb, line 17 def set!(&block) instance_eval(&block) end