Class: Factbase::Pre
- Inherits:
-
Object
- Object
- Factbase::Pre
- 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 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
- #dup ⇒ Object
-
#initialize(fb, &block) ⇒ Pre
constructor
A new instance of Pre.
- #insert ⇒ Object
- #txn(this = self) ⇒ Object
Constructor Details
#initialize(fb, &block) ⇒ Pre
Returns a new instance of Pre.
45 46 47 48 49 |
# File 'lib/factbase/pre.rb', line 45 def initialize(fb, &block) raise 'The "fb" is nil' if fb.nil? @fb = fb @block = block end |
Instance Method Details
#dup ⇒ Object
51 52 53 |
# File 'lib/factbase/pre.rb', line 51 def dup Factbase::Pre.new(@fb.dup, &@block) end |
#insert ⇒ Object
55 56 57 58 59 |
# File 'lib/factbase/pre.rb', line 55 def insert f = @fb.insert @block.call(f, self) f end |
#txn(this = self) ⇒ Object
61 62 63 |
# File 'lib/factbase/pre.rb', line 61 def txn(this = self, &) @fb.txn(this, &) end |