Class: Pact::QueryHash

Inherits:
Object
  • Object
show all
Includes:
ActiveSupportSupport, SymbolizeKeys
Defined in:
lib/pact/consumer_contract/query_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SymbolizeKeys

included, #symbolize_keys

Methods included from ActiveSupportSupport

#fix_all_the_things, #fix_json_formatting, #fix_regexp, #remove_unicode, #warn_about_regexp

Constructor Details

#initialize(query, original_string = nil, nested = false) ⇒ QueryHash

Returns a new instance of QueryHash.



13
14
15
16
17
# 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

Instance Attribute Details

#original_stringObject (readonly)

Returns the value of attribute original_string.



11
12
13
# File 'lib/pact/consumer_contract/query_hash.rb', line 11

def original_string
  @original_string
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/pact/consumer_contract/query_hash.rb', line 39

def ==(other)
  QueryHash === other && other.query == query
end

#any_key_contains_square_brackets?Boolean

Returns:

  • (Boolean)


23
24
25
# 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 = {}) ⇒ Object



27
28
29
# File 'lib/pact/consumer_contract/query_hash.rb', line 27

def as_json(opts = {})
  @hash
end

#difference(other) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
# 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?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pact/consumer_contract/query_hash.rb', line 65

def empty?
  @hash && @hash.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/pact/consumer_contract/query_hash.rb', line 35

def eql?(other)
  self == other
end

#nested?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/pact/consumer_contract/query_hash.rb', line 19

def nested?
  @nested
end

#queryObject



57
58
59
# File 'lib/pact/consumer_contract/query_hash.rb', line 57

def query
  @hash
end

#to_hashObject



69
70
71
# File 'lib/pact/consumer_contract/query_hash.rb', line 69

def to_hash
  @hash
end

#to_json(opts = {}) ⇒ Object



31
32
33
# File 'lib/pact/consumer_contract/query_hash.rb', line 31

def to_json(opts = {})
  as_json(opts).to_json(opts)
end

#to_sObject



61
62
63
# File 'lib/pact/consumer_contract/query_hash.rb', line 61

def to_s
  @hash.inspect
end