Module: Mxrb::IO::MxunitCodec

Defined in:
lib/mxrb/io/mxunit_codec.rb

Overview

Storage adapter for MPR v2 unit files.

Mendix currently stores the serialized unit payload outside SQLite. The payload is kept opaque here: known BSON payloads can be decoded, while unknown/newer encodings fail explicitly instead of being overwritten.

Defined Under Namespace

Classes: UnsupportedFormat

Class Method Summary collapse

Class Method Details

.path_for(contents_dir, uuid) ⇒ Object



16
17
18
19
# File 'lib/mxrb/io/mxunit_codec.rb', line 16

def self.path_for(contents_dir, uuid)
  hex = uuid.delete("-").downcase
  File.join(contents_dir, hex[0, 2], hex[2, 2], "#{uuid.downcase}.mxunit")
end

.read(path) ⇒ Object



21
22
23
24
25
26
# File 'lib/mxrb/io/mxunit_codec.rb', line 21

def self.read(path)
  bytes = File.binread(path)
  BsonCodec.parse(bytes)
rescue SerializationError => e
  raise UnsupportedFormat, "#{path}: unsupported .mxunit encoding (#{e.message})"
end

.serialize(doc) ⇒ Object



28
29
30
# File 'lib/mxrb/io/mxunit_codec.rb', line 28

def self.serialize(doc)
  BsonCodec.serialize(doc)
end

.write_atomic(path, bytes) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/mxrb/io/mxunit_codec.rb', line 32

def self.write_atomic(path, bytes)
  FileUtils.mkdir_p(File.dirname(path))
  temporary = "#{path}.tmp-#{Process.pid}-#{Thread.current.object_id}"
  File.binwrite(temporary, bytes)
  File.rename(temporary, path)
ensure
  FileUtils.rm_f(temporary) if temporary
end