Class: String
Instance Method Summary collapse
- #camelize(first_letter_in_uppercase = true) ⇒ Object
- #camelize!(first_letter_in_uppercase = true) ⇒ Object
- #domain(options = {}) ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(options = {}) ⇒ Object
- #ends_with?(suffix) ⇒ Boolean
- #gnix(string) ⇒ Object
- #gnix!(string) ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(options = {}) ⇒ Object
- #mixedcase? ⇒ Boolean
- #nix(string) ⇒ Object
- #nix!(string) ⇒ Object
- #pollute(delimiter = "^--^--^") ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #starts_with?(prefix) ⇒ Boolean
- #strip_tags ⇒ Object
- #strip_tags! ⇒ Object
- #strip_whitespace ⇒ Object
- #strip_whitespace! ⇒ Object
- #titleize ⇒ Object
- #titleize! ⇒ Object
- #truncate_preserving_words(options = {}) ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
- #unpollute(delimiter = "^--^--^") ⇒ Object
- #upcase? ⇒ Boolean
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(={}) url = dup url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url end |
#downcase? ⇒ 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(={}) length = [:length] || 30 separator = [:separator] || '...' return self if size <= length offset = [:offset] || 4 self[0,offset] + separator + self[-offset,offset] end |
#ends_with?(suffix) ⇒ 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( = {}) underscore. gsub(/_id$/, ""). tr('_', ' '). gsub(/([a-z\d]*)/i) { |match| match.downcase }. gsub(/^\w/) { |match| match.upcase } if .fetch(:capitalize, true) end |
#humanize!(options = {}) ⇒ Object
37 38 39 |
# File 'lib/nobiru/extensions/string_extension.rb', line 37 def humanize!( = {}) replace(humanize) end |
#mixedcase? ⇒ 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 |
#slugify ⇒ Object
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 |
#squish ⇒ Object
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
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_tags ⇒ Object
140 141 142 |
# File 'lib/nobiru/extensions/string_extension.rb', line 140 def gsub(/<\/?[^>]*>/, "") end |
#strip_tags! ⇒ Object
144 145 146 |
# File 'lib/nobiru/extensions/string_extension.rb', line 144 def replace() end |
#strip_whitespace ⇒ Object
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 |
#titleize ⇒ Object
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(={}) separator = [:separator] || '...' max_words = [: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 = [:max_chars] || 30 return self if size < max_chars out = self[0..(max_chars-1)].split(" ") out.pop out << separator out.join(" ") end end |
#underscore ⇒ Object
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
179 180 181 |
# File 'lib/nobiru/extensions/string_extension.rb', line 179 def upcase? upcase == self end |