class Pact::QueryHash

Attributes

original_string[R]

Public Class Methods

new(query, original_string = nil, nested = false) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 13
def initialize(query, original_string = nil, nested = false)
  @hash = query.nil? ? query : convert_to_hash_of_arrays(query)
  @original_string = original_string
  @nested = nested
end

Public Instance Methods

==(other) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 39
def ==(other)
  QueryHash === other && other.query == query
end
any_key_contains_square_brackets?() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 23
def any_key_contains_square_brackets?
  query.keys.any?{ |key| key =~ /\[.*\]/ }
end
as_json(opts = {}) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 27
def as_json(opts = {})
  @hash
end
difference(other) click to toggle source

other will always be a QueryString, not a QueryHash, as it will have ben created from the actual query string.

# File lib/pact/consumer_contract/query_hash.rb, line 45
def difference(other)
  require 'pact/matchers' # avoid recursive loop between this file, pact/reification and pact/matchers

  if any_key_contains_square_brackets?
    other_query_hash_non_nested = Query.parse_string_as_non_nested_query(other.query)
    Pact::Matchers.diff(query, convert_to_hash_of_arrays(other_query_hash_non_nested), allow_unexpected_keys: false)
  else
    other_query_hash = Query.parse_string(other.query)
    Pact::Matchers.diff(query, symbolize_keys(convert_to_hash_of_arrays(other_query_hash)), allow_unexpected_keys: false)
  end
end
empty?() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 65
def empty?
  @hash && @hash.empty?
end
eql?(other) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 35
def eql?(other)
  self == other
end
nested?() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 19
def nested?
  @nested
end
query() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 57
def query
  @hash
end
to_hash() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 69
def to_hash
  @hash
end
to_json(opts = {}) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 31
def to_json(opts = {})
  as_json(opts).to_json(opts)
end
to_s() click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 61
def to_s
  @hash.inspect
end

Private Instance Methods

convert_to_hash_of_arrays(query) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 75
def convert_to_hash_of_arrays(query)
  query.each_with_object({}) {|(k, v), hash| insert(hash, k, v) }
end
insert(hash, k, v) click to toggle source
# File lib/pact/consumer_contract/query_hash.rb, line 79
def insert(hash, k, v)
  if Hash === v
    v.each {|k2, v2| insert(hash, :"#{k}[#{k2}]", v2) }
  elsif Pact::ArrayLike === v
    hash[k.to_sym] = v
  else
    hash[k.to_sym] = [*v]
  end
end