Class: Factbase::Inv

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

Overview

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

For example, you can use this decorator if you want to check that every fact has when:

fb = Factbase::Inv.new(Factbase.new) do |f, fbt|
  assert !f['when'].nil?
end

The second argument passed to the block is the factbase, while the first one is the fact just touched.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: Fact, Query

Instance Method Summary collapse

Constructor Details

#initialize(fb, &block) ⇒ Inv

Returns a new instance of Inv.



28
29
30
31
# File 'lib/factbase/inv.rb', line 28

def initialize(fb, &block)
  @fb = fb
  @block = block
end

Instance Method Details

#insertObject



33
34
35
# File 'lib/factbase/inv.rb', line 33

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

#query(query, maps = nil) ⇒ Object



37
38
39
# File 'lib/factbase/inv.rb', line 37

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

#txnObject



41
42
43
44
45
# File 'lib/factbase/inv.rb', line 41

def txn
  @fb.txn do |fbt|
    yield Factbase::Inv.new(fbt, &@block)
  end
end