Class: JSON::SchemaGenerator::BruteForceRequiredSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/json/schema_generator/brute_force_required_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ BruteForceRequiredSearch

Returns a new instance of BruteForceRequiredSearch.



6
7
8
9
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 6

def initialize(data)
  @data = data.dup
  @json_path = data.is_a?(Array) ? ['$[*]'] : ['$']
end

Instance Method Details

#child_keysObject



37
38
39
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 37

def child_keys
  JsonPath.new(current_path).on(@data).map(&:keys).flatten.uniq
end

#current_pathObject



19
20
21
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 19

def current_path
  @json_path.join '.'
end

#find_requiredObject



41
42
43
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 41

def find_required
  child_keys.select {|k| required? k}
end

#popObject



15
16
17
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 15

def pop
  @json_path.pop
end

#push(key, value) ⇒ Object



11
12
13
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 11

def push(key, value)
  @json_path.push value.is_a?(Array) ? "#{key}[*]" : key
end

#required?(child_key) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 27

def required? child_key
  begin
    JsonPath.new(search_path(child_key)).on(@data).count == JsonPath.new(current_path).on(@data).count
  rescue SyntaxError
    # There are some special characters that can't be handled by JsonPath.
    # e.g. if child key is OS-DCF:diskConfig
    false
  end
end

#search_path(search_key) ⇒ Object



23
24
25
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 23

def search_path search_key
  current_path.gsub(/\[\*\]$/, "[?(@.#{search_key})]")
end