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 make sure that no property b is set to a String value:

fb = Factbase::Inv.new(Factbase.new) do |p, v| raise "b cannot be a string" if p == 'b' && v.is_a?(String) end

The first block argument is the property name (a String), the second is the value being assigned.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2024-2026 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