Module: StringManipulator

Defined in:
lib/string_manipulator.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.process_string_or_array(str, &process) ⇒ Object



24
25
26
# File 'lib/string_manipulator.rb', line 24

def self.process_string_or_array(str, &process)
  str.is_a?(Array) ? str.map(&process) : process.call(str)
end

.to_camel_case(str) ⇒ Object



17
18
19
20
21
22
# File 'lib/string_manipulator.rb', line 17

def self.to_camel_case(str)
  process = ->(str) {
    str.split(/[\s_-]+/).map(&:capitalize).join.tap { |s| s[0] = s[0].downcase }
  }
  process_string_or_array(str, &process)
end

.to_snake_case(str) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/string_manipulator.rb', line 5

def self.to_snake_case(str)
  process = ->(str) {
  str.gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .gsub(/[-\s_]+/, '_')
    .gsub(/[^a-zA-Z\d_]/, '')
    .squeeze('_')
    .sub(/^_|_$/, '')
    .downcase
  }
  process_string_or_array(str, &process)
end