PageRenderTime 21ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/chingu/inflector.rb

http://github.com/ippa/chingu
Ruby | 55 lines | 20 code | 6 blank | 29 comment | 1 complexity | 6a82b4db99970fe1ea96d56fe2ceac72 MD5 | raw file
Possible License(s): LGPL-2.1
 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
22
23module Chingu
24  module Inflector
25		
26		#
27		# "automatic_assets" -> "AutomaticAssets"
28		#
29    def Inflector.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
30      if first_letter_in_uppercase
31        lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
32      else
33        lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
34      end
35    end
36		
37    #
38    # "Chingu::GameObject" -> "GameObject"
39    #
40    def Inflector.demodulize(class_name_in_module)
41      class_name_in_module.to_s.gsub(/^.*::/, '')
42    end
43    
44		#
45		# "FireBall" -> "fire_ball"
46		#
47		def Inflector.underscore(camel_cased_word)
48			camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
49			gsub(/([a-z\d])([A-Z])/,'\1_\2').
50			tr("-", "_").
51			downcase
52		end
53		
54  end
55end