Class: LucideRuby::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache



5
6
7
8
# File 'lib/lucide_ruby/cache.rb', line 5

def initialize
  @store = {}
  @mutex = Mutex.new
end

Instance Method Details

#clear!Object



27
28
29
# File 'lib/lucide_ruby/cache.rb', line 27

def clear!
  @mutex.synchronize { @store.clear }
end

#fetch(name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/lucide_ruby/cache.rb', line 18

def fetch(name)
  cached = read(name)
  return cached if cached

  value = yield
  write(name, value)
  value
end

#preload!Object



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

def preload!
  icon_path = LucideRuby.configuration.resolved_icon_path

  unless Dir.exist?(icon_path)
    raise IconsNotSynced.new(icon_path)
  end

  Dir.glob(File.join(icon_path, "*.svg")).each do |file|
    name = File.basename(file, ".svg")
    inner_html = Icon.extract_inner_html(File.read(file))
    write(name, inner_html)
  end

  size
end

#read(name) ⇒ Object



10
11
12
# File 'lib/lucide_ruby/cache.rb', line 10

def read(name)
  @mutex.synchronize { @store[name] }
end

#sizeObject



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

def size
  @mutex.synchronize { @store.size }
end

#write(name, value) ⇒ Object



14
15
16
# File 'lib/lucide_ruby/cache.rb', line 14

def write(name, value)
  @mutex.synchronize { @store[name] = value }
end