Class: String
- Includes:
- Style
- Defined in:
- lib/mack-facets/extensions/string.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#constantize ⇒ Object
Returns a constant of the string.
- #hexdigest ⇒ Object
- #hexdigest! ⇒ Object
- #methodize ⇒ Object
- #truncate(length = 30, truncate_string = "...") ⇒ Object
- #truncate!(length = 30, truncate_string = "...") ⇒ Object
-
#uri_escape ⇒ Object
Performs URI escaping so that you can construct proper query strings faster.
-
#uri_unescape ⇒ Object
Unescapes a URI escaped string.
Class Method Details
.randomize(length = 10) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/mack-facets/extensions/string.rb', line 97 def self.randomize(length = 10) chars = ("A".."H").to_a + ("J".."N").to_a + ("P".."T").to_a + ("W".."Z").to_a + ("3".."9").to_a newpass = "" 1.upto(length) { |i| newpass << chars[rand(chars.size-1)] } return newpass.upcase end |
Instance Method Details
#constantize ⇒ Object
Returns a constant of the string.
Examples:
"User".constantize # => User
"HomeController".constantize # => HomeController
"Mack::Configuration" # => Mack::Configuration
85 86 87 |
# File 'lib/mack-facets/extensions/string.rb', line 85 def constantize Module.instance_eval("::#{self}") end |
#hexdigest ⇒ Object
89 90 91 |
# File 'lib/mack-facets/extensions/string.rb', line 89 def hexdigest Digest::SHA1.hexdigest(self) end |
#hexdigest! ⇒ Object
93 94 95 |
# File 'lib/mack-facets/extensions/string.rb', line 93 def hexdigest! self.replace(self.hexdigest) end |
#methodize ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mack-facets/extensions/string.rb', line 6 def methodize x = self # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' # get rid of the big stuff in the front/back x.strip! # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' x = x.underscore # get rid of spaces and make the _ x.gsub!(' ', '_') # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _ x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_') # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' # condense multiple 'safe' non a-z chars to just one. # ie. ___ becomes _ !!!! becomes ! etc... [' ', '_', '?', '!', "="].each do |c| x.squeeze!(c) end # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' #down case the whole thing x.downcase! # get rid of any characters at the beginning that aren't a-z while !x.match(/^[a-z]/) x.slice!(0) # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' end # let's trim this bad boy down a bit now that we've cleaned it up, somewhat. # we should do this before cleaning up the end character, because it's possible to end up with a # bad char at the end if you trim too late. x = x[0..100] if x.length > 100 # get rid of any characters at the end that aren't safe while !x.match(/[a-z0-9\?\!\=]$/) x.slice!(x.length - 1) # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' end # if we get down to a nil or an empty string raise an exception! raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == '' # let's get rid of characters that don't belong in the 'middle' of the method. orig_middle = x[1..(x.length - 2)] n_middle = orig_middle.dup ['?', '!', "="].each do |c| n_middle.gsub!(c, "_") end # the previous gsub can leave us with multiple underscores that need cleaning up. n_middle.squeeze!("_") x.gsub!(orig_middle, n_middle) x.gsub!("_=", "=") x end |
#truncate(length = 30, truncate_string = "...") ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mack-facets/extensions/string.rb', line 120 def truncate(length = 30, truncate_string = "...") if self.nil? then return end l = length - truncate_string.length if $KCODE == "NONE" self.length > length ? self[0...l] + truncate_string : self else chars = self.split(//) chars.length > length ? chars[0...l].join + truncate_string : self end end |
#truncate!(length = 30, truncate_string = "...") ⇒ Object
131 132 133 |
# File 'lib/mack-facets/extensions/string.rb', line 131 def truncate!(length = 30, truncate_string = "...") self.replace(self.truncate(length, truncate_string)) end |
#uri_escape ⇒ Object
Performs URI escaping so that you can construct proper query strings faster. Use this rather than the cgi.rb version since it’s faster. (Stolen from Camping).
107 108 109 110 111 |
# File 'lib/mack-facets/extensions/string.rb', line 107 def uri_escape self.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end |
#uri_unescape ⇒ Object
Unescapes a URI escaped string. (Stolen from Camping).
114 115 116 117 118 |
# File 'lib/mack-facets/extensions/string.rb', line 114 def uri_unescape self.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') } end |