Class: HeadMusic::Utilities::HashKey

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/utilities/hash_key.rb

Overview

Util for converting an object to a consistent hash key

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ HashKey



13
14
15
# File 'lib/head_music/utilities/hash_key.rb', line 13

def initialize(identifier)
  @original = identifier
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/head_music/utilities/hash_key.rb', line 11

def original
  @original
end

Class Method Details

.for(identifier) ⇒ Object



6
7
8
9
# File 'lib/head_music/utilities/hash_key.rb', line 6

def self.for(identifier)
  @hash_keys ||= {}
  @hash_keys[identifier] ||= new(identifier).to_sym
end

Instance Method Details

#desymbolized_stringObject (private)



32
33
34
35
36
37
38
39
40
# File 'lib/head_music/utilities/hash_key.rb', line 32

def desymbolized_string
  original.to_s
    .gsub("𝄫", "_double_flat")
    .gsub("♭", "_flat")
    .gsub("♮", "_natural")
    .gsub("♯", "_sharp")
    .gsub("#", "_sharp")
    .gsub("𝄪", "_double_sharp")
end

#normalized_stringObject (private)



23
24
25
26
# File 'lib/head_music/utilities/hash_key.rb', line 23

def normalized_string
  @normalized_string ||=
    transliterated_string.downcase.gsub(/\W+/, "_")
end

#to_symObject



17
18
19
# File 'lib/head_music/utilities/hash_key.rb', line 17

def to_sym
  normalized_string.to_sym
end

#transliterated_stringObject (private)



28
29
30
# File 'lib/head_music/utilities/hash_key.rb', line 28

def transliterated_string
  I18n.transliterate(desymbolized_string)
end