Class: Json::Schema::Subset::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/json/schema/subset/dsl.rb,
lib/json/schema/subset/dsl/version.rb

Constant Summary collapse

VERSION =
"1.2.0".freeze

Instance Method Summary collapse

Constructor Details

#initialize(type: "object", schema: {}, params: {}, ref: nil, options: nil, &block) ⇒ DSL

Returns a new instance of DSL.



7
8
9
10
11
12
13
14
15
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/json/schema/subset/dsl.rb', line 78

def method_missing(name, *args, &block)
  if name.to_s.end_with?("!")
    @params[name[0...-1]] = args[0]
    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

Instance Method Details

#array!(&block) ⇒ Object



69
70
71
72
# 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) ⇒ Object



122
123
124
# File 'lib/json/schema/subset/dsl.rb', line 122

def canon_name!(name)
  @options[:reference_name] ? @options[:reference_name].call(name.to_s) : name.to_s
end

#change_type!(type) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/json/schema/subset/dsl.rb', line 109

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!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



61
62
63
# File 'lib/json/schema/subset/dsl.rb', line 61

def components!(name)
  "#/components/#{canon_name!(name)}"
end

#cref!(name) ⇒ Object



53
54
55
# File 'lib/json/schema/subset/dsl.rb', line 53

def cref!(name)
  ref! components!(name)
end

#definitions!(name) ⇒ Object



65
66
67
# File 'lib/json/schema/subset/dsl.rb', line 65

def definitions!(name)
  "#/definitions/#{canon_name!(name)}"
end

#dref!(name) ⇒ Object



57
58
59
# File 'lib/json/schema/subset/dsl.rb', line 57

def dref!(name)
  ref! definitions!(name)
end

#ref!(name) ⇒ Object



49
50
51
# File 'lib/json/schema/subset/dsl.rb', line 49

def ref!(name)
  @ref = name.to_s
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/json/schema/subset/dsl.rb', line 74

def respond_to_missing?(name, include_private)
  true
end

#set!(&block) ⇒ Object



17
18
19
# File 'lib/json/schema/subset/dsl.rb', line 17

def set!(&block)
  instance_eval(&block)
end