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
CASE_EXTENSION =
ConfigReader.case_extension

Instance Method Summary collapse

Instance Method Details

#camelize(upper_case_first_letter = true) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/core_extended/string.rb', line 74

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



25
26
27
# File 'lib/core_extended/string.rb', line 25

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

#downcase!Object



19
20
21
22
23
# File 'lib/core_extended/string.rb', line 19

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

#downcase_ignoring_accents!Object



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

alias_method :downcase_ignoring_accents!, :downcase!

#letterizeObject



47
48
49
# File 'lib/core_extended/string.rb', line 47

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

#letterize!Object



41
42
43
44
45
# File 'lib/core_extended/string.rb', line 41

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

#normalizedObject



59
60
61
# File 'lib/core_extended/string.rb', line 59

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

#normalized!Object



51
52
53
54
55
56
57
# File 'lib/core_extended/string.rb', line 51

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

#unaccentedObject



37
38
39
# File 'lib/core_extended/string.rb', line 37

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

#unaccented!Object



29
30
31
32
33
34
35
# File 'lib/core_extended/string.rb', line 29

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

#underscoreObject



64
65
66
67
68
69
70
# File 'lib/core_extended/string.rb', line 64

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



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

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

#upcase!Object



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

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

#upcase_ignoring_accents!Object



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

alias_method :upcase_ignoring_accents!, :upcase!