Module: Coco::IconSet

Defined in:
lib/coco/icon_set.rb

Constant Summary collapse

ICON_DIR =
"app/assets/build/coco/icons"
ICON_NAMES_FILE =
"config/icons.json"
InvalidIconError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.aliasesObject



17
18
19
# File 'lib/coco/icon_set.rb', line 17

def aliases
  Coco.config.icons[:aliases]&.stringify_keys || {}
end

.all_iconsObject



21
22
23
# File 'lib/coco/icon_set.rb', line 21

def all_icons
  @all_icons ||= JSON.load_file(Engine.root.join(ICON_NAMES_FILE))
end

.custom_icon?(icon_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/coco/icon_set.rb', line 42

def custom_icon?(icon_name)
  icon_name && Coco.config.icons[:custom].include?(icon_name)
end

.icon_svg(icon_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/coco/icon_set.rb', line 46

def icon_svg(icon_name)
  icon_path = resolve_icon_path(icon_name)

  if icon_path.present? && icon_path&.exist?
    File.read(icon_path).html_safe
  end
rescue
  # Do nothing...
end

.iconsObject



9
10
11
12
13
14
15
# File 'lib/coco/icon_set.rb', line 9

def icons
  if Coco.env.development?
    [*all_icons, *Coco.config.icons[:include]].uniq
  else
    Coco.config.icons[:include]&.uniq || []
  end
end

.resolve_icon_name(name) ⇒ Object



25
26
27
# File 'lib/coco/icon_set.rb', line 25

def resolve_icon_name(name)
  aliases.fetch(name.to_s, name)
end

.resolve_icon_path(icon_name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/coco/icon_set.rb', line 29

def resolve_icon_path(icon_name)
  search_paths = [
    Engine.root.join(ICON_DIR, "custom", "#{icon_name}.svg"),
    Engine.root.join(ICON_DIR, "#{icon_name}.svg")
  ]

  search_paths.find { _1.exist? }
end

.symbol_id_for_icon(icon_name) ⇒ Object



38
39
40
# File 'lib/coco/icon_set.rb', line 38

def symbol_id_for_icon(icon_name)
  "svg_icon_#{icon_name.tr("-", "_")}" if icon_name.present?
end