/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

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