Module: Ruflet::CupertinoIconLookup

Defined in:
lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb

Constant Summary collapse

LOCAL_ICONS_JSON =
File.expand_path("cupertino/cupertino_icons.json", __dir__)

Class Method Summary collapse

Class Method Details

.candidate_names(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 51

def candidate_names(name)
  raw = name.to_s.strip
  return [] if raw.empty?

  underscored = raw.gsub(/\s+|-/, "_").downcase
  stripped = underscored.sub(/\Acupertinoicons\./i, "")
  upper = stripped.upcase

  [raw, raw.upcase, underscored, stripped, upper].uniq
end

.canonical_name_for(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 34

def canonical_name_for(value)
  return nil if value.is_a?(Integer)

  icons = icon_map
  return nil if icons.empty?

  candidate_names(value).each do |name|
    return name.downcase if icons.key?(name)
  end

  nil
end

.codepoint_for(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 11

def codepoint_for(value)
  return value if value.is_a?(Integer)

  text = value.to_s.strip
  return nil if text.empty?

  numeric = parse_numeric(text)
  return numeric unless numeric.nil?

  icons = icon_map
  candidate_names(text).each do |name|
    codepoint = icons[name]
    return codepoint unless codepoint.nil?
  end

  nil
end

.fallback_codepointObject



29
30
31
32
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 29

def fallback_codepoint
  icons = icon_map
  icons["QUESTION_CIRCLE"] || icons["QUESTION"] || 0
end

.icon_mapObject



47
48
49
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 47

def icon_map
  @icon_map ||= load_icon_map
end

.load_icon_mapObject



69
70
71
72
73
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 69

def load_icon_map
  return {} unless File.file?(LOCAL_ICONS_JSON)

  parse_icons_json(LOCAL_ICONS_JSON)
end

.parse_icons_json(path) ⇒ Object



75
76
77
78
79
80
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 75

def parse_icons_json(path)
  parsed = JSON.parse(File.read(path))
  parsed.each_with_object({}) do |(k, v), out|
    out[k.to_s.upcase] = v.to_i
  end
end

.parse_numeric(text) ⇒ Object



62
63
64
65
66
67
# File 'lib/ruflet_ui/ruflet/icons/cupertino_icon_lookup.rb', line 62

def parse_numeric(text)
  return text.to_i(16) if text.match?(/\A0x[0-9a-fA-F]+\z/)
  return text.to_i if text.match?(/\A\d+\z/)

  nil
end