Class: HeadMusic::Utilities::Case

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

Overview

Util for converting an object to a particular case

Class Method Summary collapse

Class Method Details

.to_camel_case(text) ⇒ Object



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

def self.to_camel_case(text)
  str = to_snake_case(text)
  str.split("_").map.with_index { |word, index| index.zero? ? word : word.capitalize }.join
end

.to_kebab_case(text) ⇒ Object



19
20
21
# File 'lib/head_music/utilities/case.rb', line 19

def self.to_kebab_case(text)
  to_snake_case(text).tr("_", "-")
end

.to_snake_case(text) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/head_music/utilities/case.rb', line 6

def self.to_snake_case(text)
  text.to_s
    .gsub("::", "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .tr(" ", "_")
    .gsub(/[^\w\/]+/, "_")
    .squeeze("_")
    .gsub(/^_|_$/, "")
    .downcase
end