Class: Factbase::Rules

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

Overview

A decorator of a Factbase, that checks rules on every set.

Say, you want every fact to have foo property. You want any attempt to insert a fact without this property to lead to a runtime error. Here is how:

fb = Factbase.new
fb = Factbase::Rules.new(fb, '(exists foo)')
fb.txn do |fbt|
  f = fbt.insert
  f.bar = 3 # No exception here
end # Runtime exception here (transaction won't commit)
Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: Check, Fact, Later, Query

Instance Method Summary collapse

Constructor Details

#initialize(fb, rules, check = Check.new(rules), uid: nil) ⇒ Rules

Returns a new instance of Rules.



29
30
31
32
33
34
35
36
37
# File 'lib/factbase/rules.rb', line 29

def initialize(fb, rules, check = Check.new(rules), uid: nil)
  raise 'The "fb" is nil' if fb.nil?
  @fb = fb
  raise 'The "rules" is nil' if rules.nil?
  @rules = rules
  raise 'The "check" is nil' if check.nil?
  @check = check
  @uid = uid
end

Instance Method Details

#insertObject



39
40
41
# File 'lib/factbase/rules.rb', line 39

def insert
  Fact.new(@fb.insert, @check, @fb)
end

#query(term, maps = nil) ⇒ Object



43
44
45
# File 'lib/factbase/rules.rb', line 43

def query(term, maps = nil)
  Query.new(@fb.query(term, maps), @check, self)
end

#txnObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/factbase/rules.rb', line 47

def txn
  before = @check
  later = Later.new(@uid)
  @check = later
  @fb.txn do |fbt|
    yield Factbase::Rules.new(fbt, @rules, @check, uid: @uid)
    @check = before
    fbt.query('(always)').each do |f|
      next unless later.include?(f)
      @check.it(f, @fb)
    end
  end
end