Class: Factbase::IndexedEq

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/indexed/indexed_eq.rb

Overview

Indexed term ‘eq’.

Instance Method Summary collapse

Constructor Details

#initialize(term, idx) ⇒ IndexedEq



8
9
10
11
# File 'lib/factbase/indexed/indexed_eq.rb', line 8

def initialize(term, idx)
  @term = term
  @idx = idx
end

Instance Method Details

#predict(maps, _fb, params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/factbase/indexed/indexed_eq.rb', line 13

def predict(maps, _fb, params)
  return nil if @idx.nil?
  key = [maps.object_id, @term.operands.first, @term.op]
  return unless @term.operands.first.is_a?(Symbol) && _scalar?(@term.operands[1])
  if @idx[key].nil?
    @idx[key] = {}
    prop = @term.operands.first.to_s
    maps.to_a.each do |m|
      m[prop]&.each do |v|
        @idx[key][v] = [] if @idx[key][v].nil?
        @idx[key][v].append(m)
      end
    end
  end
  vv =
    if @term.operands[1].is_a?(Symbol)
      params[@term.operands[1].to_s] || []
    else
      [@term.operands[1]]
    end
  if vv.empty?
    (maps & [])
  else
    j = vv.map { |v| @idx[key][v] || [] }.reduce(&:|)
    (maps & []) | j
  end
end