Module: Xcodeproj::Differ

Defined in:
lib/kintsugi/xcodeproj_extensions.rb

Class Method Summary collapse

Class Method Details

.array_diff(value_1, value_2, options) ⇒ Object

Replaces the implementation of ‘array_diff` with an implementation that takes into account the number of occurrences an element is found in the array. Code was mostly copied from github.com/CocoaPods/Xcodeproj/blob/51fb78a03f31614103815ce21c56dc25c044a10d/lib/xcodeproj/differ.rb#L111



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kintsugi/xcodeproj_extensions.rb', line 156

def self.array_diff(value_1, value_2, options)
  ensure_class(value_1, Array)
  ensure_class(value_2, Array)
  return nil if value_1 == value_2

  new_objects_value_1 = array_non_unique_diff(value_1, value_2)
  new_objects_value_2 = array_non_unique_diff(value_2, value_1)
  return nil if value_1.empty? && value_2.empty?

  matched_diff = {}
  if id_key = options[:id_key]
    matched_value_1 = []
    matched_value_2 = []
    new_objects_value_1.each do |entry_value_1|
      if entry_value_1.is_a?(Hash)
        id_value = entry_value_1[id_key]
        entry_value_2 = new_objects_value_2.find do |entry|
          entry[id_key] == id_value
        end
        if entry_value_2
          matched_value_1 << entry_value_1
          matched_value_2 << entry_value_2
          diff = diff(entry_value_1, entry_value_2, options)
          matched_diff[id_value] = diff if diff
        end
      end
    end

    new_objects_value_1 -= matched_value_1
    new_objects_value_2 -= matched_value_2
  end

  if new_objects_value_1.empty? && new_objects_value_2.empty?
    if matched_diff.empty?
      nil
    else
      matched_diff
    end
  else
    result = {}
    result[options[:key_1]] = new_objects_value_1 unless new_objects_value_1.empty?
    result[options[:key_2]] = new_objects_value_2 unless new_objects_value_2.empty?
    result[:diff] = matched_diff unless matched_diff.empty?
    result
  end
end

.array_non_unique_diff(value_1, value_2) ⇒ Array

Returns the difference between two arrays, taking into account the number of occurrences an element is found in both arrays.

Parameters:

  • value_1 (Array)

    First array to the difference operation.

  • value_2 (Array)

    Second array to the difference operation.

Returns:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kintsugi/xcodeproj_extensions.rb', line 213

def self.array_non_unique_diff(value_1, value_2)
  value_2_elements_by_count = value_2.reduce({}) do |hash, element|
    updated_element_hash = hash.key?(element) ? {element => hash[element] + 1} : {element => 1}
    hash.merge(updated_element_hash)
  end

  value_1_elements_by_deletions =
    value_1.to_set.map do |element|
      times_to_delete_element = value_2_elements_by_count[element] || 0
      [element, times_to_delete_element]
    end.to_h

  value_1.select do |element|
    if value_1_elements_by_deletions[element].positive?
      value_1_elements_by_deletions[element] -= 1
      next false
    end
    next true
  end
end