Class: Fontist::Utils::CacheIndex

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/fontist/utils/cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(path) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/fontist/utils/cache.rb', line 22

def self.from_file(path)
  return new unless File.exist?(path)

  content = File.read(path)

  return new if content.strip.empty? || content.strip == "---"

  from_yaml(content) || {}
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
# File 'lib/fontist/utils/cache.rb', line 36

def [](key)
  Array(items).find { |i| i.url == key }&.name
end

#[]=(key, value) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/fontist/utils/cache.rb', line 40

def []=(key, value)
  item = Array(items).find { |i| i.url == key }
  if item
    item.name = value
  else
    items << CacheIndexItem.new(url: key, name: value)
  end
end

#delete(key) ⇒ Object



49
50
51
52
# File 'lib/fontist/utils/cache.rb', line 49

def delete(key)
  item = Array(items).find { |i| i.url == key }
  items.delete(item) if item
end

#to_file(path) ⇒ Object



32
33
34
# File 'lib/fontist/utils/cache.rb', line 32

def to_file(path)
  File.write(path, to_yaml)
end