Class: OKF::Bundle::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/okf/bundle/writer.rb

Overview

Atomically writes a bundle to disk: renders concepts to a temp directory, validates it for ยง9 conformance (so a malformed bundle is never published), then promotes it into place under a lock. The disk-writing counterpart to Reader.

Defined Under Namespace

Classes: AlreadyExistsError, ValidationErrorFromResult

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle_path:, concepts:, index_files: {}, log_files: {}, overwrite: false) ⇒ Writer

Returns a new instance of Writer.



17
18
19
20
21
22
23
# File 'lib/okf/bundle/writer.rb', line 17

def initialize(bundle_path:, concepts:, index_files: {}, log_files: {}, overwrite: false)
  @bundle_path = File.expand_path(bundle_path.to_s)
  @concepts = concepts
  @index_files = index_files
  @log_files = log_files
  @overwrite = overwrite
end

Class Method Details

.call(**kwargs) ⇒ Object



13
14
15
# File 'lib/okf/bundle/writer.rb', line 13

def self.call(**kwargs)
  new(**kwargs).call
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/okf/bundle/writer.rb', line 25

def call
  FileUtils.mkdir_p(parent_path)
  with_lock do
    raise AlreadyExistsError, "bundle already exists: #{@bundle_path}" if target_exists? && !@overwrite

    temp_path = build_temp_path
    FileUtils.mkdir_p(temp_path)
    begin
      write_bundle(temp_path)
      result = Validator.call(Reader.read(temp_path))
      raise ValidationErrorFromResult.new(result), "materialized bundle is invalid" unless result.valid?

      promote(temp_path)
    ensure
      FileUtils.rm_rf(temp_path)
    end
  end

  true
end