Module: Camping::Tools

Defined in:
lib/camping/tools.rb

Class Method Summary collapse

Class Method Details

.descapeObject

A Regex to descape escaped characters. used to correct URLs that are escpaed using Rack Util’s escape function.



34
35
36
# File 'lib/camping/tools.rb', line 34

def descape
  /\\(.)/
end

.normalize_slashes(string) ⇒ Object Also known as: norms

Normalize Slashes normalizes the leading and trailing slashes of a string to only have a single trailing slash and no leading slashes.



22
23
24
25
26
27
28
# File 'lib/camping/tools.rb', line 22

def normalize_slashes(string)
  f = string.dup
  return "" if f == "" # Short circuit for blank prefixes.
  f.chop!until f[-1] != "/"
  f.slice!(0)until f[0] != "/"
  f << "/"
end

.to_snake(string) ⇒ Object

to_snake Accepts a string and snake Cases it. Also accepts symbols and coerces them into a string.



10
11
12
13
14
15
16
17
# File 'lib/camping/tools.rb', line 10

def to_snake(string)
  string = string.to_s if string.class == Symbol
  string.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end