Class: String

Inherits:
Object show all
Defined in:
lib/nobiru/extensions/string_extension.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/nobiru/extensions/string_extension.rb', line 4

def camelize(first_letter_in_uppercase = true)
  if first_letter_in_uppercase != :lower
    to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    to_s[0].chr.downcase + camelize(self)[1..-1]
  end
end

#camelize!(first_letter_in_uppercase = true) ⇒ Object



14
15
16
# File 'lib/nobiru/extensions/string_extension.rb', line 14

def camelize!(first_letter_in_uppercase = true)
  replace(camelize(first_letter_in_uppercase))
end

#domain(options = {}) ⇒ Object



79
80
81
82
# File 'lib/nobiru/extensions/string_extension.rb', line 79

def domain(options={})
  url = dup
  url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url
end

#downcase?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/nobiru/extensions/string_extension.rb', line 84

def downcase?
  downcase == self
end

#ellipsize(options = {}) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/nobiru/extensions/string_extension.rb', line 88

def ellipsize(options={})
   length    = options[:length]    || 30
   separator = options[:separator] || '...'
   return self if size <= length
   offset    = options[:offset]    || 4
   self[0,offset] + separator + self[-offset,offset]
end

#ends_with?(suffix) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/nobiru/extensions/string_extension.rb', line 20

def ends_with?(suffix)
  suffix = suffix.to_s
  self[-suffix.length, suffix.length] == suffix
end

#gnix(string) ⇒ Object



96
97
98
# File 'lib/nobiru/extensions/string_extension.rb', line 96

def gnix(string)
  gsub(string, "")
end

#gnix!(string) ⇒ Object



100
101
102
# File 'lib/nobiru/extensions/string_extension.rb', line 100

def gnix!(string)
  gsub!(string, "")
end

#humanize(options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/nobiru/extensions/string_extension.rb', line 27

def humanize(options = {})
  underscore.
  gsub(/_id$/, "").
  tr('_', ' ').
  gsub(/([a-z\d]*)/i) { |match| match.downcase }.
  gsub(/^\w/) { |match| match.upcase } if options.fetch(:capitalize, true)
end

#humanize!(options = {}) ⇒ Object



37
38
39
# File 'lib/nobiru/extensions/string_extension.rb', line 37

def humanize!(options = {})
  replace(humanize)
end

#mixedcase?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/nobiru/extensions/string_extension.rb', line 104

def mixedcase?
  !upcase? && !downcase?
end

#nix(string) ⇒ Object



108
109
110
# File 'lib/nobiru/extensions/string_extension.rb', line 108

def nix(string)
  sub(string, "")
end

#nix!(string) ⇒ Object



112
113
114
# File 'lib/nobiru/extensions/string_extension.rb', line 112

def nix!(string)
  sub!(string, "")
end

#pollute(delimiter = "^--^--^") ⇒ Object



116
117
118
# File 'lib/nobiru/extensions/string_extension.rb', line 116

def pollute(delimiter="^--^--^")
  split('').map{ |letter| "#{letter}#{delimiter}" }.join
end

#slugifyObject



120
121
122
123
124
125
126
# File 'lib/nobiru/extensions/string_extension.rb', line 120

def slugify
  gsub(/[^\x00-\x7F]+/, '').    # Remove anything non-ASCII entirely (e.g. diacritics).
  gsub(/[^\w_ \-]+/i,   '').    # Remove unwanted chars.
  gsub(/[ \-]+/i,      '-').    # No more than one of the separator in a row.
  gsub(/^\-|\-$/i,      '').    # Remove leading/trailing separator.
  downcase
end

#slugify!Object



128
129
130
# File 'lib/nobiru/extensions/string_extension.rb', line 128

def slugify!
  replace(slugify)
end

#squishObject



132
133
134
# File 'lib/nobiru/extensions/string_extension.rb', line 132

def squish
  strip.gsub(/\s+/, ' ')
end

#squish!Object



136
137
138
# File 'lib/nobiru/extensions/string_extension.rb', line 136

def squish!
  replace(squish)
end

#starts_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/nobiru/extensions/string_extension.rb', line 57

def starts_with?(prefix)
  prefix = prefix.to_s
  self[0, prefix.length] == prefix
end

#strip_tagsObject



140
141
142
# File 'lib/nobiru/extensions/string_extension.rb', line 140

def strip_tags
  gsub(/<\/?[^>]*>/, "")
end

#strip_tags!Object



144
145
146
# File 'lib/nobiru/extensions/string_extension.rb', line 144

def strip_tags!
  replace(strip_tags)
end

#strip_whitespaceObject



148
149
150
# File 'lib/nobiru/extensions/string_extension.rb', line 148

def strip_whitespace
  gnix("\t").split(" ").remove_blanks.join(" ")
end

#strip_whitespace!Object



152
153
154
# File 'lib/nobiru/extensions/string_extension.rb', line 152

def strip_whitespace!
  replace(strip_whitespace)
end

#titleizeObject



43
44
45
46
47
# File 'lib/nobiru/extensions/string_extension.rb', line 43

def titleize
  underscore.
  humanize.
  gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
end

#titleize!Object



51
52
53
# File 'lib/nobiru/extensions/string_extension.rb', line 51

def titleize!
  replace(titleize)
end

#truncate_preserving_words(options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/nobiru/extensions/string_extension.rb', line 156

def truncate_preserving_words(options={})
  separator = options[:separator] || '...'
  max_words = options[:max_words] || nil
  if max_words
    words = split
    return self if words.size < max_words
    words = words[0..(max_words-1)]
    words << separator
    words.join(" ")
  else
    max_chars = options[:max_chars] || 30
    return self if size < max_chars
    out = self[0..(max_chars-1)].split(" ")
    out.pop
    out << separator
    out.join(" ")
  end
end

#underscoreObject



64
65
66
67
68
69
70
# File 'lib/nobiru/extensions/string_extension.rb', line 64

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

#underscore!Object



74
75
76
# File 'lib/nobiru/extensions/string_extension.rb', line 74

def underscore!
  replace(underscore)
end

#unpollute(delimiter = "^--^--^") ⇒ Object



175
176
177
# File 'lib/nobiru/extensions/string_extension.rb', line 175

def unpollute(delimiter="^--^--^")
  gsub(delimiter, "")
end

#upcase?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/nobiru/extensions/string_extension.rb', line 179

def upcase?
  upcase == self
end