PageRenderTime 130ms CodeModel.GetById 86ms app.highlight 16ms RepoModel.GetById 26ms app.codeStats 0ms

/tools/Ruby/lib/ruby/gems/1.8/gems/configatron-2.8.2/lib/configatron/core_ext/string.rb

http://github.com/agross/netopenspace
Ruby | 90 lines | 48 code | 22 blank | 20 comment | 3 complexity | 281e875430a029950ed4aba955968e79 MD5 | raw file
 1class String # :nodoc:
 2  
 3  def to_configatron(*args)
 4    name_spaces = (args + self.split("::")).flatten
 5    name_spaces.collect!{|s| s.to_s.methodize}
 6    configatron.send_with_chain(name_spaces)
 7  end
 8  
 9  def underscore # :nodoc:
10    self.to_s.gsub(/::/, '/').
11      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
12      gsub(/([a-z\d])([A-Z])/,'\1_\2').
13      tr("-", "_").
14      downcase
15  end
16  
17  def methodize # :nodoc:
18    x = self
19    
20    # if we get down to a nil or an empty string raise an exception! 
21    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
22    
23    # get rid of the big stuff in the front/back
24    x.strip!
25    
26    # if we get down to a nil or an empty string raise an exception! 
27    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
28    
29    x = x.underscore
30    
31    # get rid of spaces and make the _
32    x.gsub!(' ', '_')
33    # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
34    x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
35    
36    # if we get down to a nil or an empty string raise an exception! 
37    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
38    
39    # condense multiple 'safe' non a-z chars to just one.
40    # ie. ___ becomes _ !!!! becomes ! etc...
41    [' ', '_', '?', '!', "="].each do |c|
42      x.squeeze!(c)
43    end
44    
45    # if we get down to a nil or an empty string raise an exception! 
46    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
47    
48    #down case the whole thing
49    x.downcase!
50    
51    # get rid of any characters at the beginning that aren't a-z
52    while !x.match(/^[a-z]/)
53      x.slice!(0)
54      
55      # if we get down to a nil or an empty string raise an exception! 
56      raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
57    end
58    
59    # let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
60    # we should do this before cleaning up the end character, because it's possible to end up with a 
61    # bad char at the end if you trim too late.
62    x = x[0..100] if x.length > 100
63    
64    # get rid of any characters at the end that aren't safe
65    while !x.match(/[a-z0-9\?\!\=]$/)
66      x.slice!(x.length - 1)
67      # if we get down to a nil or an empty string raise an exception! 
68      raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
69    end
70    
71    # if we get down to a nil or an empty string raise an exception! 
72    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
73    
74    # let's get rid of characters that don't belong in the 'middle' of the method.
75    orig_middle = x[1..(x.length - 2)]
76    n_middle = orig_middle.dup
77    
78    ['?', '!', "="].each do |c|
79      n_middle.gsub!(c, "_")
80    end
81    
82    # the previous gsub can leave us with multiple underscores that need cleaning up.
83    n_middle.squeeze!("_")
84    
85    x.gsub!(orig_middle, n_middle)
86    x.gsub!("_=", "=")
87    x
88  end
89  
90end