
ComparableSet only allows a single object of a given class, choosing the object with the greatest value. This was mainly created for Requirements, so that, e.g., two X11Dependencies of differing strictness don't both end up in the same requirement set. Fixes Homebrew/homebrew#15240.
25 lines
529 B
Ruby
25 lines
529 B
Ruby
require 'set'
|
|
|
|
class ComparableSet < Set
|
|
def add new
|
|
# smileys only
|
|
return super new unless new.respond_to? :>
|
|
|
|
objs = find_all { |o| o.class == new.class }
|
|
objs.each do |o|
|
|
return self if o > new
|
|
delete o
|
|
end
|
|
super new
|
|
end
|
|
|
|
alias_method :<<, :add
|
|
|
|
# Set#merge bypasses enumerating the set's contents,
|
|
# so the subclassed #add would never be called
|
|
def merge enum
|
|
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
|
|
enum.each { |o| add(o) }
|
|
end
|
|
end
|