module ViewModel::Utils::Collections

Public Class Methods

count_by(enumerable) { |el| ... } click to toggle source
# File lib/view_model/utils/collections.rb, line 5
def self.count_by(enumerable)
  enumerable.each_with_object({}) do |el, counts|
    key = yield(el)

    unless key.nil?
      counts[key] = (counts[key] || 0) + 1
    end
  end
end

Public Instance Methods

contains_exactly?(other) click to toggle source
# File lib/view_model/utils/collections.rb, line 16
def contains_exactly?(other)
  mine   = count_by { |x| x }
  theirs = other.count_by { |x| x }
  mine == theirs
end
count_by(&by) click to toggle source
# File lib/view_model/utils/collections.rb, line 22
def count_by(&by)
  Collections.count_by(self, &by)
end
duplicates() click to toggle source
# File lib/view_model/utils/collections.rb, line 30
def duplicates
  duplicates_by { |x| x }
end
duplicates_by(&by) click to toggle source
# File lib/view_model/utils/collections.rb, line 26
def duplicates_by(&by)
  count_by(&by).delete_if { |_, count| count == 1 }
end