Class: Fitting::Cover::JSONSchema
- Inherits:
-
Object
- Object
- Fitting::Cover::JSONSchema
- Defined in:
- lib/fitting/cover/json_schema.rb
Instance Method Summary collapse
- #add_super_key(vbn, new_key) ⇒ Object
- #clone_hash(old_json_schema) ⇒ Object
- #combinations ⇒ Object
-
#initialize(json_schema) ⇒ JSONSchema
constructor
A new instance of JSONSchema.
- #json_schemas ⇒ Object
- #modify_json_shema(value, vbn) ⇒ Object
- #new_keys(json_schema) ⇒ Object
- #required(json_schema) ⇒ Object
- #super_each(json_schema, old_keys_hash) ⇒ Object
- #super_merge(vbn, asd, old_json_schema) ⇒ Object
Constructor Details
#initialize(json_schema) ⇒ JSONSchema
Returns a new instance of 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/fitting/cover/json_schema.rb', line 29 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
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fitting/cover/json_schema.rb', line 60 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 |
#combinations ⇒ Object
74 75 76 77 |
# File 'lib/fitting/cover/json_schema.rb', line 74 def combinations json_schemas @combinations end |
#json_schemas ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/fitting/cover/json_schema.rb', line 8 def json_schemas return @json_schemas if @json_schemas @json_schemas, @combinations = required(@json_schema) return @json_schemas unless @json_schema['properties'] super_each(@json_schema['properties'], 'properties' => nil) @json_schemas end |
#modify_json_shema(value, vbn) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/fitting/cover/json_schema.rb', line 50 def modify_json_shema(value, vbn) qwe = required(value) qwe[0].map do |asd| new_json_shema = clone_hash(@json_schema) super_merge(vbn, asd, new_json_shema) @json_schemas += [new_json_shema] end @combinations += qwe[1] end |
#new_keys(json_schema) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fitting/cover/json_schema.rb', line 94 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 |
#required(json_schema) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fitting/cover/json_schema.rb', line 79 def required(json_schema) combinations = [] json_schemas = new_keys(json_schema).inject([]) do |new_json_shemas, 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 combinations.push(['required', new_key]) new_json_shemas.push(new_json_shema) end [json_schemas, combinations] end |
#super_each(json_schema, old_keys_hash) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fitting/cover/json_schema.rb', line 18 def super_each(json_schema, old_keys_hash) json_schema.each do |key, value| new_keys_hash = clone_hash(old_keys_hash) add_super_key(new_keys_hash, key) if value.is_a?(Hash) modify_json_shema(value, new_keys_hash) super_each(value, new_keys_hash) end end end |
#super_merge(vbn, asd, old_json_schema) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fitting/cover/json_schema.rb', line 39 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 |