Class: Factbase::Defn

Inherits:
TermBase show all
Defined in:
lib/factbase/terms/defn.rb

Overview

Factbase::Defn is responsible for defining new terms in the Factbase system.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ Defn

Constructor.

Parameters:

  • operands (Array)

    Operands



15
16
17
18
# File 'lib/factbase/terms/defn.rb', line 15

def initialize(operands)
  super()
  @operands = operands
end

Instance Method Details

#evaluate(_fact, _maps, _fb) ⇒ Object

Evaluate term on a fact.

Parameters:

Returns:

  • (Object)

    Term definition result



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/factbase/terms/defn.rb', line 25

def evaluate(_fact, _maps, _fb)
  assert_args(2)
  fn = @operands[0]
  raise "A symbol expected as first argument of 'defn'" unless fn.is_a?(Symbol)
  raise "Can't use '#{fn}' name as a term" if Factbase::Term.instance_methods(true).include?(fn)
  raise "Term '#{fn}' is already defined" if Factbase::Term.private_instance_methods(false).include?(fn)
  raise "The '#{fn}' is a bad name for a term" unless fn.match?(/^[a-z_]+$/)
  e = "class Factbase::Term\nprivate\ndef #{fn}(fact, maps, fb)\n#{@operands[1]}\nend\nend"
  # rubocop:disable Security/Eval
  eval(e)
  # rubocop:enable Security/Eval
  true
end