/lib/chingu/inflector.rb

http://github.com/ippa/chingu · Ruby · 55 lines · 20 code · 6 blank · 29 comment · 1 complexity · 6a82b4db99970fe1ea96d56fe2ceac72 MD5 · raw file

  1. #--
  2. #
  3. # Chingu -- OpenGL accelerated 2D game framework for Ruby
  4. # Copyright (C) 2009 ippa / ippa@rubylicio.us
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. #
  20. #++
  21. module Chingu
  22. module Inflector
  23. #
  24. # "automatic_assets" -> "AutomaticAssets"
  25. #
  26. def Inflector.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  27. if first_letter_in_uppercase
  28. lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  29. else
  30. lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  31. end
  32. end
  33. #
  34. # "Chingu::GameObject" -> "GameObject"
  35. #
  36. def Inflector.demodulize(class_name_in_module)
  37. class_name_in_module.to_s.gsub(/^.*::/, '')
  38. end
  39. #
  40. # "FireBall" -> "fire_ball"
  41. #
  42. def Inflector.underscore(camel_cased_word)
  43. camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  44. gsub(/([a-z\d])([A-Z])/,'\1_\2').
  45. tr("-", "_").
  46. downcase
  47. end
  48. end
  49. end