Class: Judges::Impex
Overview
Import/Export of factbases.
This class provides functionality for importing and exporting Factbase objects to and from binary files. It handles file I/O operations with proper logging and error handling.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#export(fb) ⇒ Object
Export factbase to file.
-
#import(strict: true) ⇒ Factbase
Import factbase from file.
-
#import_to(fb) ⇒ Object
Import factbase from file into existing factbase.
-
#initialize(loog, file) ⇒ Impex
constructor
Initialize a new Impex instance.
Constructor Details
#initialize(loog, file) ⇒ Impex
Initialize a new Impex instance.
28 29 30 31 |
# File 'lib/judges/impex.rb', line 28 def initialize(loog, file) @loog = loog @file = file end |
Instance Method Details
#export(fb) ⇒ Object
Export factbase to file.
Exports the given Factbase instance to the file specified during initialization. Creates any necessary parent directories automatically. The operation is timed and logged with file size and fact count information.
96 97 98 99 100 101 102 |
# File 'lib/judges/impex.rb', line 96 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.
Creates a new Factbase instance and imports data from the file specified during initialization. The operation is timed and logged. If the file doesn’t exist, behavior depends on the strict parameter.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/judges/impex.rb', line 49 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.
Imports data from the file into an existing Factbase instance rather than creating a new one. This is useful when you need to merge data into an already populated factbase. The operation is timed and logged.
76 77 78 79 80 81 82 |
# File 'lib/judges/impex.rb', line 76 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 |