Class: Factbase::Pre

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

Overview

A decorator of a Factbase, that runs a provided block on every insert.

For example, you can use this decorator if you want to put some properties into every fact that gets into the factbase:

fb = Factbase::Pre.new(Factbase.new) do |f, fbt|
  f.when = Time.now
end

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

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, &block) ⇒ Pre

Returns a new instance of Pre.



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

def initialize(fb, &block)
  raise 'The "fb" is nil' if fb.nil?
  @fb = fb
  @block = block
end

Instance Method Details

#insertObject



34
35
36
37
38
# File 'lib/factbase/pre.rb', line 34

def insert
  f = @fb.insert
  @block.call(f, self)
  f
end

#txnObject



40
41
42
43
44
# File 'lib/factbase/pre.rb', line 40

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