Class: Fitting::Cover::JSONSchema

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

Instance Method Summary collapse

Constructor Details

#initialize(json_schema) ⇒ JSONSchema



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

def initialize(json_schema)
  @json_schema = json_schema
end

Instance Method Details

#add_super_key(vbn, new_key) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/fitting/cover/json_schema.rb', line 37

def add_super_key(vbn, new_key)
  vbn.each do |key, value|
    if value
      add_super_key(value, new_key)
    else
      vbn[key] = { new_key => nil }
    end
  end
end

#clone_hash(old_json_schema) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fitting/cover/json_schema.rb', line 58

def clone_hash(old_json_schema)
  new_json_schema = {}
  old_json_schema.each do |key, value|
    if value.is_a?(Hash)
      new_json_schema.merge!(key => clone_hash(value))
    elsif value
      new_json_schema.merge!(key => value.clone)
    else
      new_json_schema.merge!(key => nil)
    end
  end
  new_json_schema
end

#combiObject



8
9
10
11
12
13
14
15
16
# File 'lib/fitting/cover/json_schema.rb', line 8

def combi
  return @combinations if @combinations
  @combinations = new_required(@json_schema)

  return @combinations unless @json_schema['properties']
  @combinations = new_super_each(@json_schema['properties'], { 'properties' => nil }, @json_schema, @combinations, 'properties')

  @combinations
end

#new_keys(json_schema) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/fitting/cover/json_schema.rb', line 86

def new_keys(json_schema)
  return [] unless json_schema && json_schema['properties']
  all = json_schema['properties'].keys.map(&:to_s)
  old = json_schema['required']
  if old
    all - old
  else
    all
  end
end

#new_required(json_schema) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fitting/cover/json_schema.rb', line 72

def new_required(json_schema)
  res = []
  new_keys(json_schema).map do |new_key|
    new_json_shema = json_schema.dup
    if new_json_shema['required']
      new_json_shema['required'] += [new_key]
    else
      new_json_shema['required'] = [new_key]
    end
    res.push([new_json_shema, ['required', new_key]])
  end
  res
end

#new_super_each(json_schema, old_keys_hash, lol_schema, combinations, old_key) ⇒ Object



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

def new_super_each(json_schema, old_keys_hash, lol_schema, combinations, old_key)
  json_schema.each do |key, value|
    next unless value.is_a?(Hash)

    new_keys_hash = clone_hash(old_keys_hash)
    add_super_key(new_keys_hash, key)

    combinations = new_super_each(value, new_keys_hash, lol_schema, combinations, [old_key, key].compact.join('.'))

    qwe = new_required(value)
    qwe.map do |asd|
      new_json_shema = clone_hash(lol_schema)
      super_merge(new_keys_hash, asd[0], new_json_shema)
      combinations.push([new_json_shema, [asd[1][0], [old_key, key, asd[1][1]].compact.join('.')]])
    end
  end
  combinations
end

#super_merge(vbn, asd, old_json_schema) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/fitting/cover/json_schema.rb', line 47

def super_merge(vbn, asd, old_json_schema)
  vbn.each do |key, value|
    if value
      super_merge(value, asd, old_json_schema[key])
    else
      old_json_schema[key].merge!(asd)
    end
  end
  old_json_schema
end