Module: Ruflet::IconNames

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

Overview

Derives Ruby constant names from icon identifiers. Implemented byte-wise rather than with regexps: the tables are built for thousands of icons at load time, and this path must stay fast on every supported runtime.

Class Method Summary collapse

Class Method Details

.constant_for(name) ⇒ Object



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

def constant_for(name)
  text = +""
  prev_underscore = false
  name.to_s.each_byte do |byte|
    if (byte >= 48 && byte <= 57) || (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122)
      text << byte.chr
      prev_underscore = false
    elsif !prev_underscore
      text << "_"
      prev_underscore = true
    end
  end
  text = text[1..-1] while text.start_with?("_")
  text = text[0...-1] while text.end_with?("_")
  first = text.getbyte(0)
  text = "ICON_#{text}" if first && first >= 48 && first <= 57
  text
end