Class: Judges::Impex

Inherits:
Object show all
Defined in:
lib/judges/impex.rb

Overview

Import/Export of factbases.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog, file) ⇒ Impex

Initialize.

Parameters:

  • loog (Loog)

    Logging facility

  • file (String)

    File path for import/export operations



20
21
22
23
# File 'lib/judges/impex.rb', line 20

def initialize(loog, file)
  @loog = loog
  @file = file
end

Instance Method Details

#export(fb) ⇒ Object

Export factbase to file.

Parameters:

  • fb (Factbase)

    The factbase to export



55
56
57
58
59
60
61
# File 'lib/judges/impex.rb', line 55

def export(fb)
  elapsed(@loog, level: Logger::INFO) do
    FileUtils.mkdir_p(File.dirname(@file))
    File.binwrite(@file, fb.export)
    throw :"Factbase exported to #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
  end
end

#import(strict: true) ⇒ Factbase

Import factbase from file.

Parameters:

  • strict (Boolean) (defaults to: true)

    Whether to raise error if file doesn’t exist

Returns:

  • (Factbase)

    The imported factbase



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/judges/impex.rb', line 28

def import(strict: true)
  fb = Factbase.new
  if File.exist?(@file)
    elapsed(@loog, level: Logger::INFO) do
      fb.import(File.binread(@file))
      throw :"The factbase imported from #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
    end
  else
    raise "The factbase is absent at #{@file.to_rel}" if strict
    @loog.info("Nothing to import from #{@file.to_rel} (file not found)")
  end
  fb
end

#import_to(fb) ⇒ Object

Import factbase from file into existing factbase.

Parameters:

  • fb (Factbase)

    The factbase to import into

Raises:

  • (RuntimeError)

    If file doesn’t exist



45
46
47
48
49
50
51
# File 'lib/judges/impex.rb', line 45

def import_to(fb)
  raise "The factbase is absent at #{@file.to_rel}" unless File.exist?(@file)
  elapsed(@loog, level: Logger::INFO) do
    fb.import(File.binread(@file))
    throw :"The factbase loaded from #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
  end
end