examples/sequel_example.rb in search_lingo-2.0.0.pre2 vs examples/sequel_example.rb in search_lingo-2.0.0.pre3
- old
+ new
@@ -1,5 +1,7 @@
+# frozen-string-literal: true
+
require 'sequel'
require 'sqlite3'
# DB = Sequel.sqlite
#
@@ -15,16 +17,17 @@
# Date :due_date, null: false
# end
class CategoryParser # :nodoc:
def call(token, chain)
- if token.modifier == 'cat'
- # This is kind of broken. The categories table will be joined once each
- # time this parser matches a token.
- chain.join(:categories, id: :category_id)
- .where Sequel.qualify('categories', 'name') => token.term
- end
+ return nil unless token.modifier == 'cat'
+
+ # This is not an ideal example. Sequel will join the categories table for
+ # each token that matches. I'm ignoring the problem since this is only an
+ # example.
+ category_name = Sequel.qualify :categories, :name
+ chain.join(:categories, id: :category_id).where category_name => token.term
end
end
class TaskSearch < SearchLingo::AbstractSearch # :nodoc:
parser CategoryParser.new
@@ -55,10 +58,11 @@
token.match %r{\A(?<m>\d{1,2})/(?<d>\d{1,2})/(?<y>\d{2}\d{2}?)\z} do |m|
begin
date = Date.parse "#{m[:y]}/#{m[:m]}/#{m[:d]}"
chain.where due_date: date
rescue ArgumentError
- # Date.parse raised an ArgumentError
+ # Fail if Date.parse raises an ArgumentError
+ nil
end
end
end
# Match tasks with names containing the given string.