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:



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

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)
  hash = YAML.safe_load(yaml, permitted_classes: [Symbol])
  cache = Model::ModelElement.from_hash(hash, root_path: root_path)

  if cache.version != version
    raise Error.new("Cache version mismatch, cache version is #{cache.version}, Expressir version is #{version}")
  end

  cache.content
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)


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

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,
  )

  hash = cache.to_hash(root_path: root_path)
  yaml = YAML.dump(hash)
  yaml_compressed = Zlib::Deflate.deflate(yaml)

  File.binwrite(file, yaml_compressed)
  nil
end