Class: Clipcellar::GroongaSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/clipcellar/groonga_searcher.rb

Class Method Summary collapse

Class Method Details

.search(database, words, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/clipcellar/groonga_searcher.rb', line 20

def search(database, words, options)
  clipboards = database.clipboards
  selected_clipboards = clipboards.select do |clipboard|
    expression = nil
    words.each do |word|
      sub_expression = (clipboard.text =~ word)
      if expression.nil?
        expression = sub_expression
      else
        expression &= sub_expression
      end
    end

    if options[:mtime]
      base_date = (Time.now - (options[:mtime] * 60 * 60 * 24))
      mtime_expression = clipboard.date > base_date
      if expression.nil?
        expression = mtime_expression
      else
        expression &= mtime_expression
      end
    end

    expression
  end

  order = options[:reverse] ? "ascending" : "descending"
  sorted_clipboards = selected_clipboards.sort([{
                                                  :key => "created_at",
                                                  :order => order,
                                                }])

  sorted_clipboards
end