Skip to content

Commit

Permalink
#11 cache fb in global
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 5, 2024
1 parent d950b5c commit cbb4d20
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/fbe/fb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@
require 'factbase/rules'
require_relative '../fbe'

def Fbe.fb
rules = Dir.glob(File.join('rules', '*.fe')).map { |f| File.read(f) }
fb = Factbase::Rules.new(
$fb,
"(and \n#{rules.join("\n")}\n)",
uid: '_id'
)
fb = Factbase::Pre.new(fb) do |f|
max = $fb.query('(eq _id (max _id))').each.to_a.first
f._id = (max.nil? ? 0 : max._id) + 1
f._time = Time.now
f._version = "#{Factbase::VERSION}/#{Judges::VERSION}/#{$options.judges_action_version}"
def Fbe.fb(global: $global)
global[:fb] ||= begin
rules = Dir.glob(File.join('rules', '*.fe')).map { |f| File.read(f) }
fb = Factbase::Rules.new(
$fb,
"(and \n#{rules.join("\n")}\n)",
uid: '_id'
)
fb = Factbase::Pre.new(fb) do |f|
max = $fb.query('(eq _id (max _id))').each.to_a.first
f._id = (max.nil? ? 0 : max._id) + 1
f._time = Time.now
f._version = "#{Factbase::VERSION}/#{Judges::VERSION}/#{$options.judges_action_version}"
end
Factbase::Looged.new(fb, Loog::NULL)
end
Factbase::Looged.new(fb, Loog::NULL)
end
3 changes: 3 additions & 0 deletions test/fbe/test_conclude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
class TestConclude < Minitest::Test
def test_with_defaults
$fb = Factbase.new
$global = {}
$options = Judges::Options.new
$loog = Loog::NULL
Fbe.conclude do
Expand All @@ -63,6 +64,7 @@ def test_draw

def test_maybe
$fb = Factbase.new
$global = {}
$options = Judges::Options.new
$loog = Loog::NULL
Fbe.fb.insert.foo = 1
Expand All @@ -84,6 +86,7 @@ def test_maybe

def test_consider
$fb = Factbase.new
$global = {}
$options = Judges::Options.new
$loog = Loog::NULL
Fbe.fb.insert.foo = 1
Expand Down
1 change: 1 addition & 0 deletions test/fbe/test_fb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class TestFb < Minitest::Test
def test_simple
$fb = Factbase.new
$global = {}
$options = Judges::Options.new
Fbe.fb.insert.foo = 1
Fbe.fb.insert.bar = 2
Expand Down

0 comments on commit cbb4d20

Please sign in to comment.