Class String
In: lib/mack-facets/extensions/string.rb
Parent: Object

Methods

Included Modules

Style

Public Class methods

[Source]

     # File lib/mack-facets/extensions/string.rb, line 95
 95:   def self.randomize(length = 10)
 96:     chars = ("A".."H").to_a + ("J".."N").to_a + ("P".."T").to_a + ("W".."Z").to_a + ("3".."9").to_a
 97:     newpass = ""
 98:     1.upto(length) { |i| newpass << chars[rand(chars.size-1)] }
 99:     return newpass.upcase
100:   end

Public Instance methods

Returns a constant of the string.

Examples:

  "User".constantize # => User
  "HomeController".constantize # => HomeController
  "Mack::Configuration" # => Mack::Configuration

[Source]

    # File lib/mack-facets/extensions/string.rb, line 83
83:   def constantize
84:     Module.instance_eval("::#{self}")
85:   end

[Source]

    # File lib/mack-facets/extensions/string.rb, line 87
87:   def hexdigest
88:     Digest::SHA1.hexdigest(self)
89:   end

[Source]

    # File lib/mack-facets/extensions/string.rb, line 91
91:   def hexdigest!
92:     self.replace(self.hexdigest)
93:   end

[Source]

    # File lib/mack-facets/extensions/string.rb, line 4
 4:   def methodize
 5:     x = self
 6:     
 7:     # if we get down to a nil or an empty string raise an exception! 
 8:     raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
 9:     
10:     # get rid of the big stuff in the front/back
11:     x.strip!
12:     
13:     # if we get down to a nil or an empty string raise an exception! 
14:     raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
15:     
16:     x = x.underscore
17:     
18:     # get rid of spaces and make the _
19:     x.gsub!(' ', '_')
20:     # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
21:     x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
22:     
23:     # if we get down to a nil or an empty string raise an exception! 
24:     raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
25:     
26:     # condense multiple 'safe' non a-z chars to just one.
27:     # ie. ___ becomes _ !!!! becomes ! etc...
28:     [' ', '_', '?', '!', "="].each do |c|
29:       x.squeeze!(c)
30:     end
31:     
32:     # if we get down to a nil or an empty string raise an exception! 
33:     raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
34:     
35:     #down case the whole thing
36:     x.downcase!
37:     
38:     # get rid of any characters at the beginning that aren't a-z
39:     while !x.match(/^[a-z]/)
40:       x.slice!(0)
41:       
42:       # if we get down to a nil or an empty string raise an exception! 
43:       raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
44:     end
45:     
46:     # let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
47:     # we should do this before cleaning up the end character, because it's possible to end up with a 
48:     # bad char at the end if you trim too late.
49:     x = x[0..100] if x.length > 100
50:     
51:     # get rid of any characters at the end that aren't safe
52:     while !x.match(/[a-z0-9\?\!\=]$/)
53:       x.slice!(x.length - 1)
54:       # if we get down to a nil or an empty string raise an exception! 
55:       raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
56:     end
57:     
58:     # if we get down to a nil or an empty string raise an exception! 
59:     raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
60:     
61:     # let's get rid of characters that don't belong in the 'middle' of the method.
62:     orig_middle = x[1..(x.length - 2)]
63:     n_middle = orig_middle.dup
64:     
65:     ['?', '!', "="].each do |c|
66:       n_middle.gsub!(c, "_")
67:     end
68:     
69:     # the previous gsub can leave us with multiple underscores that need cleaning up.
70:     n_middle.squeeze!("_")
71:     
72:     x.gsub!(orig_middle, n_middle)
73:     x.gsub!("_=", "=")
74:     x
75:   end

[Source]

     # File lib/mack-facets/extensions/string.rb, line 118
118:   def truncate(length = 30, truncate_string = "...")
119:     if self.nil? then return end
120:     l = length - truncate_string.length
121:     if $KCODE == "NONE"
122:       self.length > length ? self[0...l] + truncate_string : self
123:     else
124:       chars = self.split(//)
125:       chars.length > length ? chars[0...l].join + truncate_string : self
126:     end
127:   end

[Source]

     # File lib/mack-facets/extensions/string.rb, line 129
129:   def truncate!(length = 30, truncate_string = "...")
130:     self.replace(self.truncate(length, truncate_string))
131:   end

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).

[Source]

     # File lib/mack-facets/extensions/string.rb, line 105
105:   def uri_escape
106:     self.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
107:       '%'+$1.unpack('H2'*$1.size).join('%').upcase
108:     }.tr(' ', '+')
109:   end

Unescapes a URI escaped string. (Stolen from Camping).

[Source]

     # File lib/mack-facets/extensions/string.rb, line 112
112:   def uri_unescape
113:     self.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
114:       [$1.delete('%')].pack('H*')
115:     }
116:   end

[Validate]