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

Returns a new instance of UnorderedArrayMatcher.



80
81
82
# File 'lib/rspec/json_expectations/matchers.rb', line 80

def initialize(array)
  @array = array
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



76
77
78
# File 'lib/rspec/json_expectations/matchers.rb', line 76

def array
  @array
end

Instance Method Details

#all?(&blk) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rspec/json_expectations/matchers.rb', line 106

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

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



101
102
103
104
# File 'lib/rspec/json_expectations/matchers.rb', line 101

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

#match(errors, actual, prefix) ⇒ Object



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

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



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

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



110
111
112
# File 'lib/rspec/json_expectations/matchers.rb', line 110

def unwrap_array
  array
end