Module: Factbase::Term::Aliases

Included in:
Factbase::Term
Defined in:
lib/factbase/terms/aliases.rb

Overview

Aliases terms.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#as(fact, maps) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/factbase/terms/aliases.rb', line 31

def as(fact, maps)
  assert_args(2)
  a = @operands[0]
  raise "A symbol expected as first argument of 'as'" unless a.is_a?(Symbol)
  vv = the_values(1, fact, maps)
  vv&.each { |v| fact.send("#{a}=", v) }
  true
end

#join(fact, maps) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/factbase/terms/aliases.rb', line 40

def join(fact, maps)
  assert_args(2)
  jumps = @operands[0]
  raise "A string expected as first argument of 'join'" unless jumps.is_a?(String)
  jumps = jumps.split(',')
    .map(&:strip)
    .map { |j| j.split('<=').map(&:strip) }
    .map { |j| j.size == 1 ? [j[0], j[0]] : j }
  term = @operands[1]
  raise "A term expected, but '#{term}' provided" unless term.is_a?(Factbase::Term)
  subset = Factbase::Query.new(maps, Mutex.new, term.to_s).each(fact).to_a
  subset.each do |s|
    jumps.each do |to, from|
      s[from]&.each do |v|
        fact.send("#{to}=", v)
      end
    end
  end
  true
end