Class: String

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

Constant Summary collapse

ACCENTS_MAPPING =
ConfigReader.accents_mapping
SIMILAR_CHARACTER_MAPPING =
ConfigReader.similar_character_mapping

Instance Method Summary collapse

Instance Method Details

#camelize(upper_case_first_letter = true) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/core_extended/string.rb', line 71

def camelize(upper_case_first_letter=true)
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  if upper_case_first_letter
    split('_').map{|e| e.capitalize}.join
  else
    split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
  end
end

#downcaseObject



22
23
24
# File 'lib/core_extended/string.rb', line 22

def downcase
  self.dup.tap(&:downcase!)
end

#downcase!Object



17
18
19
20
# File 'lib/core_extended/string.rb', line 17

def downcase!
  ACCENTS_MAPPING.values.each { |map| tr! map['upcase'], map['downcase'] }
  downcase_ignoring_accents!
end

#downcase_ignoring_accents!Object



16
# File 'lib/core_extended/string.rb', line 16

alias_method :downcase_ignoring_accents!, :downcase!

#letterizeObject



44
45
46
# File 'lib/core_extended/string.rb', line 44

def letterize
  self.dup.tap(&:letterize!)
end

#letterize!Object



38
39
40
41
42
# File 'lib/core_extended/string.rb', line 38

def letterize!
  SIMILAR_CHARACTER_MAPPING.each do |letter, similar_chars|
    tr! similar_chars, letter.to_s
  end
end

#normalizedObject



56
57
58
# File 'lib/core_extended/string.rb', line 56

def normalized
  self.dup.tap(&:normalized!)
end

#normalized!Object



48
49
50
51
52
53
54
# File 'lib/core_extended/string.rb', line 48

def normalized!
  self.strip!
  self.gsub! /\s/, '_'
  self.unaccented!
  self.letterize!
  self.downcase!
end

#unaccentedObject



34
35
36
# File 'lib/core_extended/string.rb', line 34

def unaccented
  self.dup.tap(&:unaccented!)
end

#unaccented!Object



26
27
28
29
30
31
32
# File 'lib/core_extended/string.rb', line 26

def unaccented!
  ACCENTS_MAPPING.each do |letter,map|
    tr! map['upcase'], letter
    tr! map['downcase'], letter.downcase
  end
  nil
end

#underscoreObject



61
62
63
64
65
66
67
# File 'lib/core_extended/string.rb', line 61

def underscore
  self.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

#upcaseObject



12
13
14
# File 'lib/core_extended/string.rb', line 12

def upcase
  self.dup.tap(&:upcase!)
end

#upcase!Object



7
8
9
10
# File 'lib/core_extended/string.rb', line 7

def upcase!
  ACCENTS_MAPPING.values.each { |map| tr! map['downcase'], map['upcase'] }
  upcase_ignoring_accents!
end

#upcase_ignoring_accents!Object



6
# File 'lib/core_extended/string.rb', line 6

alias_method :upcase_ignoring_accents!, :upcase!