Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/string.rb

Constant Summary collapse

DISALLOWED_CHARACTERS =
/[\-\_\s.,?\'\":\/;\\]/

Instance Method Summary collapse

Instance Method Details

#humanizeObject



4
5
6
# File 'lib/core_ext/string.rb', line 4

def humanize
  gsub(DISALLOWED_CHARACTERS, " ").split(" ").map(&:capitalize).join("")
end

#sanitizeObject



30
31
32
# File 'lib/core_ext/string.rb', line 30

def sanitize
  gsub(/_{2,}/, "_").gsub(DISALLOWED_CHARACTERS, "_")
end

#underscoreObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/core_ext/string.rb', line 8

def underscore
  u = ""
  chars = sanitize.split("")
  unless chars.count == 0
    while chars.first.match(DISALLOWED_CHARACTERS)
      chars.delete_at(0)
    end

    while chars.last.match(DISALLOWED_CHARACTERS)
      chars.delete_at(chars.count - 1)
    end
  end

  chars.each_with_index do |c, i|
    if c.match(/[A-Za-z]/) && i > 0 && c == c.upcase
      u += " "
    end
    u += c.downcase
  end
  u.gsub(/\s/, "_")
end