Class: Factbase::Rules
- Inherits:
-
Object
- Object
- Factbase::Rules
- 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. = 3 # No exception here
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
- #dup ⇒ Object
-
#initialize(fb, rules, check = Check.new(rules), uid: nil) ⇒ Rules
constructor
A new instance of Rules.
- #insert ⇒ Object
- #query(query) ⇒ Object
- #txn(this = self) ⇒ Object
Constructor Details
#initialize(fb, rules, check = Check.new(rules), uid: nil) ⇒ Rules
Returns a new instance of Rules.
46 47 48 49 50 51 52 53 54 |
# File 'lib/factbase/rules.rb', line 46 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
#dup ⇒ Object
56 57 58 |
# File 'lib/factbase/rules.rb', line 56 def dup Factbase::Rules.new(@fb.dup, @rules, @check, uid: @uid) end |
#insert ⇒ Object
60 61 62 |
# File 'lib/factbase/rules.rb', line 60 def insert Fact.new(@fb.insert, @check) end |
#query(query) ⇒ Object
64 65 66 |
# File 'lib/factbase/rules.rb', line 64 def query(query) Query.new(@fb.query(query), @check) end |
#txn(this = self) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/factbase/rules.rb', line 68 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 |