Module: CommandSearch::Memory

Defined in:
lib/command_search/backends/memory.rb

Class Method Summary collapse

Class Method Details

.check(item, ast) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/command_search/backends/memory.rb', line 45

def check(item, ast)
  ast.all? do |node|
    val = node[:value]
    case node[:type]
    when :colon
      command_check(item, val)
    when :compare
      compare_check(item, node)
    when :not
      !val.all? { |v| check(item, [v]) }
    when :or
      val.any? { |v| check(item, [v]) }
    when :and
      val.all? { |v| check(item, [v]) }
    end
  end
end

.command_check(item, val) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/command_search/backends/memory.rb', line 5

def command_check(item, val)
  cmd = val[0][:value]
  search = val[1][:value]
  item_val = item[cmd.to_sym] || item[cmd.to_s]
  type = val[1][:type]
  type = Boolean if type == :existence && search == true
  if type == Boolean
    !!item_val == search
  elsif type == :existence
    item_val == nil
  elsif !item_val
    return false
  elsif search.is_a?(Regexp)
    item_val.to_s[search]
    # for versions ruby 2.4.0 (2016-12-25) and up, match? is much faster
    # item_val.to_s.match?(search)
  elsif type == Time
    item_time = item_val.to_time
    return false if search.nil?
    search.first <= item_time && item_time < search.last
  else
    item_val == search
  end
end

.compare_check(item, node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/command_search/backends/memory.rb', line 30

def compare_check(item, node)
  cmd_val = node[:value].first[:value]
  item_val = item[cmd_val.to_sym] || item[cmd_val.to_s]
  return unless item_val
  val = node[:value].last[:value]
  if val.is_a?(Time)
    item_val = item_val.to_time
  elsif node[:compare_across_fields]
    val = item[val.to_sym] || item[val.to_s]
  end
  return unless val
  fn = node[:nest_op].to_sym.to_proc
  fn.call(item_val.to_f, val.to_f)
end