Class: RSpec::JsonExpectations::Matchers::UnorderedArrayMatcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rspec/json_expectations/matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ UnorderedArrayMatcher



78
79
80
# File 'lib/rspec/json_expectations/matchers.rb', line 78

def initialize(array)
  @array = array
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



74
75
76
# File 'lib/rspec/json_expectations/matchers.rb', line 74

def array
  @array
end

Instance Method Details

#all?(&blk) ⇒ Boolean



104
105
106
# File 'lib/rspec/json_expectations/matchers.rb', line 104

def all?(&blk)
  array.each_with_index.all?(&blk)
end

#check_for_missing(missing, item, index, ok) ⇒ Object



99
100
101
102
# File 'lib/rspec/json_expectations/matchers.rb', line 99

def check_for_missing(missing, item, index, ok)
  missing << { item: item, index: index } unless ok
  ok
end

#match(errors, actual, prefix) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rspec/json_expectations/matchers.rb', line 82

def match(errors, actual, prefix)
  missing_items = []
  errors[prefix.join("/")] = { missing: missing_items }

  all? do |expected_item, index|
    match_one(missing_items, expected_item, index, actual)
  end
end

#match_one(missing, item, index, actual) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/rspec/json_expectations/matchers.rb', line 91

def match_one(missing, item, index, actual)
  check_for_missing(missing, item, index,
    actual.any? do |actual_item|
      JsonTraverser.traverse({}, item, actual_item, false)
    end
  )
end

#unwrap_arrayObject



108
109
110
# File 'lib/rspec/json_expectations/matchers.rb', line 108

def unwrap_array
  array
end