Class: Fitting::Cover::JSONSchemaEnum

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/cover/json_schema_enum.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_schema) ⇒ JSONSchemaEnum

Returns a new instance of JSONSchemaEnum.



4
5
6
7
# File 'lib/fitting/cover/json_schema_enum.rb', line 4

def initialize(json_schema)
  @json_schema = json_schema
  @combinations = []
end

Instance Method Details

#combiObject



9
10
11
12
13
14
# File 'lib/fitting/cover/json_schema_enum.rb', line 9

def combi
  inception(@json_schema, @combinations).each do |combination|
    combination[0] = @json_schema.merge(combination[0])
    combination[1] = ['enum', combination[1]]
  end
end

#inception(json_schema, combinations) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fitting/cover/json_schema_enum.rb', line 16

def inception(json_schema, combinations)
  json_schema.each do |key, value|
    if key == 'enum' && value.size > 1
      schema = json_schema.dup
      one_of = schema.delete('enum')
      one_of.each_index do |index|
        combinations.push([schema.merge('enum' => [one_of[index]]), "enum.#{one_of[index]}"])
      end
    elsif value.is_a?(Hash)
      com = inception(value, [])
      com.each do |combination|
        combination[0] = { key => value.merge(combination[0]) }
        combination[1] = "#{key}.#{combination[1]}"
      end
      combinations += com
    end
  end

  combinations
end