Class: Tokite::SearchQuery
- Inherits:
-
Object
- Object
- Tokite::SearchQuery
- Defined in:
- app/models/tokite/search_query.rb
Defined Under Namespace
Classes: ParseError, Parser
Constant Summary collapse
- DEFAULT_FIELDS =
%i(title body)
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(query) ⇒ SearchQuery
constructor
A new instance of SearchQuery.
- #match?(doc) ⇒ Boolean
Constructor Details
#initialize(query) ⇒ SearchQuery
Returns a new instance of SearchQuery.
43 44 45 46 |
# File 'app/models/tokite/search_query.rb', line 43 def initialize(query) @query = query @tree = Array.wrap(self.class.parse(query)) end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'app/models/tokite/search_query.rb', line 5 def query @query end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
5 6 7 |
# File 'app/models/tokite/search_query.rb', line 5 def tree @tree end |
Class Method Details
.parse(query) ⇒ Object
37 38 39 40 41 |
# File 'app/models/tokite/search_query.rb', line 37 def self.parse(query) Array.wrap(parser.parse(query)) rescue Parslet::ParseFailed => e raise ParseError, e end |
.parser ⇒ Object
33 34 35 |
# File 'app/models/tokite/search_query.rb', line 33 def self.parser @parser ||= Tokite::SearchQuery::Parser.new end |
Instance Method Details
#match?(doc) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/tokite/search_query.rb', line 48 def match?(doc) tree.all? do |word| field = word[:field] if field targets = doc[field.to_sym] ? [doc[field.to_sym].downcase] : [] else targets = DEFAULT_FIELDS.map{|field| doc[field]&.downcase }.compact end if word[:regexp_word] regexp = Regexp.compile(word[:regexp_word].to_s, Regexp::IGNORECASE) matched = targets.any?{|text| regexp.match?(text) } else value = word[:word].to_s.downcase matched = targets.any?{|text| text.index(value) } end word[:exclude].present? ? !matched : matched end end |