Class: Factbase::Inv

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

Overview

TODO:

#644:30min Add a test that validates YARD docstring examples match the actual API signatures. The docstring in this class was recently fixed because the block signature was documented as |f, fbt| but the actual implementation passes |property, value|. A general test that parses docstrings and checks their examples against the real method signatures would prevent this class of bugs from recurring across the codebase.

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.



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

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

Instance Method Details

#insertObject



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

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

#query(query, maps = nil) ⇒ Object



45
46
47
# File 'lib/factbase/inv.rb', line 45

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

#txnObject



49
50
51
52
53
# File 'lib/factbase/inv.rb', line 49

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