Class: HakumiComponents::Icon::Loader

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/services/hakumi_components/icon/loader.rb

Constant Summary collapse

Cache =
T.type_alias { T::Hash[String, T.nilable(String)] }
ICONS_BASE_PATH =
T.let(
  if defined?(HakumiComponents::Engine)
    HakumiComponents::Engine.root.join("app", "assets", "icons")
  else
    ::Rails.root.join("app", "assets", "icons")
  end,
  Pathname
)
@@cache =
T.let({}, Cache)

Class Method Summary collapse

Class Method Details

.cacheObject



21
22
23
# File 'app/services/hakumi_components/icon/loader.rb', line 21

def self.cache
  @@cache
end

.clear_cache!Object



53
54
55
# File 'app/services/hakumi_components/icon/loader.rb', line 53

def self.clear_cache!
  @@cache = {}
end

.exists?(name, theme = :outlined) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/services/hakumi_components/icon/loader.rb', line 38

def self.exists?(name, theme = :outlined)
  !load(name, theme).nil?
end

.list_icons(theme = :outlined) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/services/hakumi_components/icon/loader.rb', line 43

def self.list_icons(theme = :outlined)
  theme_dir = theme_directory(theme)
  return [] unless theme_dir.exist?

  Dir.glob(theme_dir.join("*.svg").to_s).map do |path|
    File.basename(path, ".svg").gsub("-", "_")
  end.sort
end

.load(name, theme = :outlined) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'app/services/hakumi_components/icon/loader.rb', line 26

def self.load(name, theme = :outlined)
  return nil if name.nil?

  cache_key = "#{name}_#{theme}"
  return cache[cache_key] if cache.key?(cache_key)

  svg = read_icon_file(name, theme)
  cache[cache_key] = svg
  svg
end