Class: Tzispa::Utils::String

Inherits:
String
  • Object
show all
Defined in:
lib/tzispa/utils/string.rb

Constant Summary collapse

NAMESPACE_SEPARATOR =
'::'
CLASSIFY_SEPARATOR =
'_'
UNDERSCORE_SEPARATOR =
'/'
UNDERSCORE_DIVISION_TARGET =
'\1_\2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object



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

def self.camelize(str)
  self.new(str.to_s).camelize
end

.constantize(str) ⇒ Object



16
17
18
# File 'lib/tzispa/utils/string.rb', line 16

def self.constantize(str)
  self.new(str.to_s).constantize
end

.underscore(str) ⇒ Object



42
43
44
# File 'lib/tzispa/utils/string.rb', line 42

def self.underscore(str)
  self.new(str.to_s).underscore
end

Instance Method Details

#camelizeObject



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

def camelize
  self.split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join
end

#camelize!Object



38
39
40
# File 'lib/tzispa/utils/string.rb', line 38

def camelize!
  self.split(CLASSIFY_SEPARATOR).collect!{ |w| w.capitalize }.join
end

#constantizeObject



20
21
22
23
24
25
26
27
28
# File 'lib/tzispa/utils/string.rb', line 20

def constantize
  names = self.split(NAMESPACE_SEPARATOR)
  names.shift if names.empty? || names.first.empty?
  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#transliterate(locale = nil) ⇒ Object

Replace accents in the string using I18n.transliterate



67
68
69
# File 'lib/tzispa/utils/string.rb', line 67

def transliterate(locale=nil)
  I18n.transliterate(self, ({locale: locale} if locale))
end

#underscoreObject



46
47
48
49
50
51
52
53
54
# File 'lib/tzispa/utils/string.rb', line 46

def underscore
  self.dup.tap { |s|
    s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
    s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
    s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
    s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
    s.downcase!
  }
end

#underscore!Object



56
57
58
59
60
61
62
63
64
# File 'lib/tzispa/utils/string.rb', line 56

def underscore!
  self.tap { |s|
    s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
    s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
    s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
    s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
    s.downcase!
  }
end

#urlize(options = {}) ⇒ Object

Convert a string to a format suitable for a URL without ever using escaped characters. It calls strip, transliterate, downcase (optional) then removes the spaces (optional) and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).

Options

  • :downcase => call downcase on the string (defaults to true)

  • :convert_spaces => Convert space to underscore (defaults to false)

  • :regexp => The regexp matching characters that will be removed (defaults to /[^-_A-Za-z0-9]/)



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tzispa/utils/string.rb', line 80

def urlize(options = {})
  options[:downcase] ||= true
  options[:convert_spaces] ||= true
  options[:regexp] ||= /[^-_A-Za-z0-9]/

  self.transliterate(options[:locale]).strip.tap { |str|
    str.downcase! if options[:downcase]
    str.gsub!(/\ /,'_') if options[:convert_spaces]
    str.gsub!(options[:regexp], '')
  }
end