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 = Factabase::Rules.new(fb, '(exists foo)')
fb.txn do |fbt|
  f = fbt.insert
  f.bar = 3
end # Runtime exception here (transaction won't commit)
Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 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.



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

def initialize(fb, rules, check = Check.new(rules), uid: nil)
  @fb = fb
  @rules = rules
  @check = check
  @uid = uid
end

Instance Method Details

#dupObject



49
50
51
# File 'lib/factbase/rules.rb', line 49

def dup
  Factbase::Rules.new(@fb.dup, @rules, @check, uid: @uid)
end

#exportObject



79
80
81
# File 'lib/factbase/rules.rb', line 79

def export
  @fb.export
end

#import(bytes) ⇒ Object



83
84
85
# File 'lib/factbase/rules.rb', line 83

def import(bytes)
  @fb.import(bytes)
end

#insertObject



57
58
59
# File 'lib/factbase/rules.rb', line 57

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

#query(query) ⇒ Object



61
62
63
# File 'lib/factbase/rules.rb', line 61

def query(query)
  Query.new(@fb.query(query), @check)
end

#sizeObject



53
54
55
# File 'lib/factbase/rules.rb', line 53

def size
  @fb.size
end

#txn(this = self) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/factbase/rules.rb', line 65

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