Class: Tokite::SearchQuery

Inherits:
Object
  • Object
show all
Defined in:
app/models/tokite/search_query.rb

Defined Under Namespace

Classes: ParseError, Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ SearchQuery

Returns a new instance of SearchQuery.



41
42
43
44
# File 'app/models/tokite/search_query.rb', line 41

def initialize(query)
  @query = query
  @tree = Array.wrap(self.class.parse(query))
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'app/models/tokite/search_query.rb', line 5

def query
  @query
end

#treeObject (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



35
36
37
38
39
# File 'app/models/tokite/search_query.rb', line 35

def self.parse(query)
  Array.wrap(parser.parse(query))
rescue Parslet::ParseFailed => e
  raise ParseError, e
end

.parserObject



31
32
33
# File 'app/models/tokite/search_query.rb', line 31

def self.parser
  @parser ||= Tokite::SearchQuery::Parser.new
end

Instance Method Details

#match?(doc) ⇒ Boolean

Returns:

  • (Boolean)


46
47
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 46

def match?(doc)
  tree.all? do |word|
    field = word[:field]
    if word[:regexp_word]
      regexp = word[:regexp_word].to_s
      if field
        doc[field.to_sym]&.match(regexp)
      else
        doc.values.any?{|text| text.match(regexp) }
      end
    else
      value = word[:word].to_s
      if field
        doc[field.to_sym]&.index(value)
      else
        doc.values.any?{|text| text.index(value) }
      end
    end
  end
end