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



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

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



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

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

#[]=(key, value) ⇒ Object



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

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



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

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

#to_file(path) ⇒ Object



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

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