Class: JSON::SchemaGenerator::BruteForceRequiredSearch
- Inherits:
-
Object
- Object
- JSON::SchemaGenerator::BruteForceRequiredSearch
- Defined in:
- lib/json/schema_generator/brute_force_required_search.rb
Instance Method Summary collapse
- #child_keys ⇒ Object
- #current_path ⇒ Object
- #find_required ⇒ Object
-
#initialize(data) ⇒ BruteForceRequiredSearch
constructor
A new instance of BruteForceRequiredSearch.
- #pop ⇒ Object
- #push(key, value) ⇒ Object
- #required?(child_key) ⇒ Boolean
- #search_path(search_key) ⇒ Object
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 = ['$'] end |
Instance Method Details
#child_keys ⇒ Object
35 36 37 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 35 def child_keys JsonPath.new(current_path).on(@data).map(&:keys).flatten.uniq end |
#current_path ⇒ Object
23 24 25 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 23 def current_path @json_path.join '.' end |
#find_required ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 39 def find_required required = [] child_keys.each do |child_key| required << child_key if required? child_key end required end |
#pop ⇒ Object
19 20 21 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 19 def pop @json_path.pop end |
#push(key, value) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 11 def push(key, value) if value.is_a? Array @json_path.push "#{key}[*]" else @json_path.push key end end |
#required?(child_key) ⇒ Boolean
31 32 33 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 31 def required? child_key JsonPath.new(search_path(child_key)).on(@data).count == JsonPath.new(current_path).on(@data).count end |
#search_path(search_key) ⇒ Object
27 28 29 |
# File 'lib/json/schema_generator/brute_force_required_search.rb', line 27 def search_path search_key current_path.gsub(/\[\*\]$/, "[?(@.#{search_key})]") end |