class Openapi3Parser::CautiousDig

Public Class Methods

call(*args) click to toggle source
# File lib/openapi3_parser/cautious_dig.rb, line 7
def self.call(*args)
  new.call(*args)
end

Public Instance Methods

call(collection, *segments) click to toggle source
# File lib/openapi3_parser/cautious_dig.rb, line 11
def call(collection, *segments)
  segments.inject(collection) do |next_depth, segment|
    break unless next_depth

    if next_depth.respond_to?(:keys)
      hash_like(next_depth, segment)
    elsif next_depth.respond_to?(:[])
      array_like(next_depth, segment)
    end
  end
end

Private Instance Methods

array_like(item, segment) click to toggle source
# File lib/openapi3_parser/cautious_dig.rb, line 30
def array_like(item, segment)
  index = if segment.is_a?(String) && segment =~ /\A\d+\z/
            segment.to_i
          else
            segment
          end
  index.is_a?(Integer) ? item[index] : nil
end
hash_like(item, segment) click to toggle source
# File lib/openapi3_parser/cautious_dig.rb, line 25
def hash_like(item, segment)
  key = item.keys.find { |k| segment == k || segment.to_s == k.to_s }
  item[key]
end