Class: String
- Inherits:
-
Object
- Object
- String
- 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
- #camelize(upper_case_first_letter = true) ⇒ Object
- #downcase ⇒ Object
- #downcase! ⇒ Object
- #downcase_ignoring_accents! ⇒ Object
- #letterize ⇒ Object
- #letterize! ⇒ Object
- #normalized ⇒ Object
- #normalized! ⇒ Object
- #unaccented ⇒ Object
- #unaccented! ⇒ Object
- #underscore ⇒ Object
- #upcase ⇒ Object
- #upcase! ⇒ Object
- #upcase_ignoring_accents! ⇒ Object
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 |
#downcase ⇒ Object
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! |
#letterize ⇒ Object
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 |
#normalized ⇒ Object
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 |
#unaccented ⇒ Object
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 |
#underscore ⇒ Object
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 |
#upcase ⇒ Object
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! |