Class: Expressir::Express::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/expressir/express/cache.rb

Class Method Summary collapse

Class Method Details

.from_file(file, root_path: nil, test_overwrite_version: nil) ⇒ Model::ModelElement

Load Express model from a cache file

Parameters:

  • file (String)

    cache file path

  • root_path (String) (defaults to: nil)

    Express repository root path, to be prepended to Express file paths if loading a portable cache file

  • test_overwrite_version (String) (defaults to: nil)

    don’t use, only for tests

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/expressir/express/cache.rb', line 34

def self.from_file(file, root_path: nil, test_overwrite_version: nil)
  version = test_overwrite_version || VERSION

  yaml_compressed = File.binread(file)
  yaml = Zlib::Inflate.inflate(yaml_compressed)
  cache = Model::Cache.from_yaml(yaml)

  if cache.version != version
    raise Error::CacheVersionMismatchError.new(cache.version, version)
  end

  cache
end

.to_file(file, content, root_path: nil, test_overwrite_version: nil) ⇒ nil

Save Express model into a cache file

Parameters:

  • file (String)

    cache file path

  • content (Model::ModelElement)

    Express model

  • root_path (String) (defaults to: nil)

    Express repository root path, to be stripped from Express file paths to create a portable cache file

  • test_overwrite_version (String) (defaults to: nil)

    don’t use, only for tests

Returns:

  • (nil)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/expressir/express/cache.rb', line 12

def self.to_file(file, content, root_path: nil,
test_overwrite_version: nil)
  version = test_overwrite_version || VERSION

  cache = Model::Cache.new(
    version: version,
    content: content,
    root_path: root_path,
  )

  yaml = cache.to_yaml
  yaml_compressed = Zlib::Deflate.deflate(yaml)

  File.binwrite(file, yaml_compressed)
  nil
end