module Eco::API::UseCases::GraphQL::Helpers::Location class TagsRemap class TagsSet class << self def attr_compare(*attrs) attrs.each do |attr| meth = "#{attr}".to_sym define_method meth do |value| set.send(meth, to_set(value)) end end end def attr_operate(*attrs) attrs.each do |attr| meth = "#{attr}".to_sym define_method meth do |value| self.class.new(set.send(meth, to_set(value))) end end end end COMPARE_METHODS = %i[< <= == > >=].freeze OPERATE_METHIDS = %i[& + - ^].freeze attr_compare *COMPARE_METHODS attr_operate *OPERATE_METHIDS attr_reader :tags, :set def initialize(tags) @ini_tags = value_to_a(tags) @set = to_set(tags) end def length set.length end def tags ini_tags.compact.map(&:upcase) end def to_a tags end def to_s tags.join('|') end def empty? set.empty? end def include?(value) value = value.to_s.strip return false if value.empty? set.include?(value) end def include_any?(value) value.to_a.any? {|tag| self.include?(tag)} end def subset_of?(value) set.subset?(to_set(value)) end def superset_of?(value) set.superset?(to_set(value)) end protected def ini_tags @ini_tags end private def value_to_a(value) return value.ini_tags.dup if value.is_a?(self.class) return value.dup if value.is_a?(Array) return value.to_a if value.is_a?(Set) raise ArgumentError, "Expecting #{self.class}, Set or Array. Given: #{value.class}" end def to_set(value) values = value_to_a(value) Set.new.merge(values.compact.map(&:upcase)) end end end end