Sha256: 6683334a549a5af3741d5b020c5f03a30f99bb87b1e18b0fa1befadddaac6a56

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

class << RSpec::OpenAPI::HashHelper = Object.new
  def paths_to_all_fields(obj)
    case obj
    when Hash
      obj.each.flat_map do |k, v|
        k = k.to_s
        [[k]] + paths_to_all_fields(v).map { |x| [k, *x] }
      end
    else
      []
    end
  end

  def matched_paths(obj, selector)
    selector_parts = selector.split('.').map(&:to_s)
    paths_to_all_fields(obj).select do |key_parts|
      key_parts.size == selector_parts.size && key_parts.zip(selector_parts).all? do |kp, sp|
        kp == sp || (sp == '*' && !kp.nil?)
      end
    end
  end

  def matched_paths_deeply_nested(obj, begin_selector, end_selector)
    path_depth_sizes = paths_to_all_fields(obj).map(&:size).uniq
    path_depth_sizes.map do |depth|
      diff = depth - begin_selector.count('.') - end_selector.count('.')
      if diff >= 0
        selector = "#{begin_selector}.#{'*.' * diff}#{end_selector}"
        matched_paths(obj, selector)
      else
        []
      end
    end.flatten(1)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rspec-openapi-0.11.0 lib/rspec/openapi/hash_helper.rb
rspec-openapi-0.10.0 lib/rspec/openapi/hash_helper.rb
rspec-openapi-0.9.0 lib/rspec/openapi/hash_helper.rb
rspec-openapi-0.8.1 lib/rspec/openapi/hash_helper.rb
rspec-openapi-0.8.0 lib/rspec/openapi/hash_helper.rb