Module: Truck::StringInflections

Extended by:
StringInflections
Included in:
StringInflections
Defined in:
lib/truck/string_inflections.rb

Instance Method Summary collapse

Instance Method Details

#to_camel_case(str) ⇒ Object



5
6
7
8
9
10
# File 'lib/truck/string_inflections.rb', line 5

def to_camel_case(str)
  str = "_#{str}"
  str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase }
  str.gsub!('/', '::')
  str
end

#to_snake_case(str) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/truck/string_inflections.rb', line 12

def to_snake_case(str)
  str = str.gsub '::', '/'
  # Convert FOOBar => FooBar
  str.gsub! %r{[[:upper:]]{2,}} do |uppercase|
    bit = uppercase[0]
    bit << uppercase[1..-1].downcase
    bit
  end
  # Convert FooBar => foo_bar
  str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel|
    bit = camel[0]
    bit << '_'
    bit << camel[1..-1].downcase
  end
  str.downcase!
  str
end