Skip to content

Commit

Permalink
#9 once
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 12, 2024
1 parent c385b12 commit 5ae41b6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ Layout/EmptyLineAfterGuardClause:
Enabled: false
Layout/CaseIndentation:
Enabled: false
Naming/MethodParameterName:
MinNameLength: 2
61 changes: 61 additions & 0 deletions lib/judges/fb/once.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

# Copyright (c) 2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Returns a decorated global factbase, which only touches facts once
def once(fb)
Factbase::Once.new(fb, $judge)
end

# Runs only once.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class Factbase::Once
def initialize(fb, func)
@fb = fb
@func = func
end

def query(expr)
expr = "(and #{expr} (not (eq seen '#{@func}')))"
After.new(@fb.query(expr), @func)
end

def insert
@fb.insert
end

# What happens after a fact is processed.
class After
def initialize(query, func)
@query = query
@func = func
end

def each
@query.each do |f|
yield f
f.seen = @func
end
end
end
end
2 changes: 2 additions & 0 deletions lib/judges/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require 'yaml'
require_relative '../judges'
require_relative '../judges/fb/once'

# A single pack.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand All @@ -37,6 +38,7 @@ def initialize(dir)
# Run it with the given Factbase and environment variables.
def run(fbase, env)
$fb = fbase
$judge = File.basename(File.dirname(@dir))
env.each do |k, v|
# rubocop:disable Security/Eval
eval("$#{k} = '#{v}'", binding, __FILE__, __LINE__) # $foo = 42
Expand Down
8 changes: 8 additions & 0 deletions test/test_pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ def test_run_isolated
assert_equal(1, fb2.size)
end
end

def test_with_supplemenary_functions
Dir.mktmpdir do |d|
File.write(File.join(d, 'x.rb'), 'once($fb).insert')
pack = Judges::Pack.new(d)
pack.run(Factbase.new, {})
end
end
end

0 comments on commit 5ae41b6

Please sign in to comment.