Module: Factbase::IndexedTerm

Defined in:
lib/factbase/indexed/indexed_term.rb

Overview

Term with an index.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#predict(maps, params) ⇒ Object



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/factbase/indexed/indexed_term.rb', line 14

def predict(maps, params)
  case @op
  when :eq
    if @operands[0].is_a?(Symbol) && _scalar?(@operands[1])
      key = [maps.object_id, @operands[0], @op]
      if @idx[key].nil?
        @idx[key] = {}
        maps.to_a.each do |m|
          m[@operands[0].to_s]&.each do |v|
            @idx[key][v] = [] if @idx[key][v].nil?
            @idx[key][v].append(m)
          end
        end
      end
      vv =
        if @operands[1].is_a?(Symbol)
          sym = @operands[1].to_s.gsub(/^\$/, '')
          params[sym] || []
        else
          [@operands[1]]
        end
      vv.map { |v| @idx[key][v] || [] }.reduce(&:|)
    else
      maps.to_a
    end
  when :and
    parts = @operands.map { |o| o.predict(maps, params) }
    if parts.include?(nil)
      maps
    else
      parts.reduce(&:&)
    end
  when :or
    @operands.map { |o| o.predict(maps, params) }.reduce(&:|)
  when :join, :as
    nil
  else
    maps.to_a
  end
end