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



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

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



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

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

#[]=(key, value) ⇒ Object



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

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



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

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

#to_file(path) ⇒ Object



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

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