Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/string.rb

Instance Method Summary collapse

Instance Method Details

#camelizeObject



54
55
56
57
58
# File 'lib/hiptest-publisher/string.rb', line 54

def camelize
  normalized = self.normalize.split('_')
  normalized.map! {|w| w.empty? ? "" : "#{w[0].upcase}#{w[1..-1]}"}
  normalized.join
end

#camelize_lowerObject



60
61
62
63
64
65
66
67
# File 'lib/hiptest-publisher/string.rb', line 60

def camelize_lower
  camelized = self.camelize
  if camelized.empty?
    ""
  else
    "#{camelized[0].downcase}#{camelized[1..-1]}"
  end
end

#camelize_upperObject



69
70
71
# File 'lib/hiptest-publisher/string.rb', line 69

def camelize_upper
  self.camelize
end

#clear_extensionObject



73
74
75
# File 'lib/hiptest-publisher/string.rb', line 73

def clear_extension
  self.split('.')[0]
end

#literateObject



6
7
8
9
# File 'lib/hiptest-publisher/string.rb', line 6

def literate
  I18n.enforce_available_locales = false
  I18n.transliterate(self)
end

#normalize(keep_dashes = false, keep_spaces = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hiptest-publisher/string.rb', line 11

def normalize(keep_dashes=false, keep_spaces=false)
  literated = self.literate
  literated.strip!

  if keep_spaces
    literated.gsub!(/\s+/, ' ')
    literated.gsub!(/[^a-zA-Z0-9_\- "']/, '')
  else
    literated.gsub!(/\s+/, '_')
    if keep_dashes
      literated.gsub!(/[^a-zA-Z0-9_\-]/, '')
    else
      literated.gsub!(/\W/, '')
    end
  end
  literated
end

#normalize_lowerObject



29
30
31
32
33
# File 'lib/hiptest-publisher/string.rb', line 29

def normalize_lower
  normalized = self.normalize
  normalized.downcase!
  normalized
end

#normalize_with_dashesObject



35
36
37
# File 'lib/hiptest-publisher/string.rb', line 35

def normalize_with_dashes
  self.normalize(true)
end

#normalize_with_spacesObject



39
40
41
# File 'lib/hiptest-publisher/string.rb', line 39

def normalize_with_spaces
  self.normalize(false, true)
end

#underscoreObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/hiptest-publisher/string.rb', line 43

def underscore
  # based on:
  # http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
  normalized = self.normalize
  normalized.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  normalized.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  normalized.tr!("-", "_")
  normalized.downcase!
  normalized
end