Simplify filter API a bit

Not doing the tricks with `is_positive` anymore, instead just calling different functions altogether at the call site.

Also, instead of classes, I feel like this is a lot more concise to just write as class methods that create certain instances of a trivial `Filter` data class. Without the tricks of `is_positive` in play, the value of classes goes way down imo.
This commit is contained in:
Matchu 2023-07-26 11:46:10 -07:00 committed by Matchu
parent 3e3aa6a126
commit 66db73748a

View file

@ -35,13 +35,15 @@ class Item
case key
when 'name'
filters << NameFilter.new(value, locale, is_positive)
filters << (is_positive ?
Filter.name_includes(value, locale) :
Filter.name_excludes(value, locale))
when 'is'
case value
when 'nc'
filters << NCFilter.new(is_positive)
filters << (is_positive ? Filter.is_nc : Filter.is_not_nc)
when 'np'
filters << NPFilter.new(is_positive)
filters << (is_positive ? Filter.is_np : Filter.is_not_np)
else
message = I18n.translate('items.search.errors.not_found.label',
:label => "is:#{value}")
@ -67,56 +69,50 @@ class Item
private
class NameFilter
def initialize(value, locale, is_positive)
@value = value
@locale = locale
@is_positive = is_positive
# A Filter is basically a wrapper for an Item scope, with extra info about
# how to convert it into a search query string.
class Filter
def initialize(query, text)
@query = query
@text = text
end
def to_query
@is_positive ?
Item.name_includes(@value, @locale) :
Item.name_excludes(@value, @locale)
@query
end
def to_s
sign = @is_positive ? '' : '-'
if /\s/.match(@value)
sign + '"' + @value + '"'
else
sign + @value
end
end
@text
end
class NCFilter
def initialize(is_positive)
@is_positive = is_positive
def inspect
"#<#{self.class.name} #{@text.inspect}>"
end
def to_query
@is_positive ? Item.is_nc : Item.is_np
def self.name_includes(value, locale)
text = /\s/.match(value) ? '"' + value + '"' : value
self.new Item.name_includes(value, locale), text
end
def to_s
sign = @is_positive ? '' : '-'
sign + 'is:nc'
end
def self.name_excludes(value, locale)
text = '-' + (/\s/.match(value) ? '"' + value + '"' : value)
self.new Item.name_excludes(value, locale), text
end
class NPFilter
def initialize(is_positive)
@is_positive = is_positive
def self.is_nc
self.new Item.is_nc, 'is:nc'
end
def to_query
@is_positive ? Item.is_np : Item.is_nc
def self.is_not_nc
self.new Item.is_np, '-is:nc'
end
def to_s
sign = @is_positive ? '' : '-'
sign + 'is:np'
def self.is_np
self.new Item.is_np, 'is:np'
end
def self.is_not_np
self.new Item.is_nc, '-is:np'
end
end
end