Module: Pact::Utils::String

Extended by:
String
Included in:
String
Defined in:
lib/pact/utils/string.rb

Instance Method Summary collapse

Instance Method Details

#camelcase(string, *separators) ⇒ Object

ripped from rubyworks/facets, thank you



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pact/utils/string.rb', line 10

def camelcase(string, *separators)
  case separators.first
  when Symbol, TrueClass, FalseClass, NilClass
    first_letter = separators.shift
  end

  separators = ['_', '\s'] if separators.empty?

  str = string.dup

  separators.each do |s|
    str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
  end

  case first_letter
  when :upper, true
    str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
  when :lower, false
    str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
  end

  str
end