Sha256: 615b5a5374b8e563e1c536070daec62b4e0e15417c88874496aa1a8e582c426d

Contents?: true

Size: 1003 Bytes

Versions: 2

Compression:

Stored size: 1003 Bytes

Contents

class String

  # Converts a string into a valid ruby class or module name
  # This method is geared toward code reflection.
  #
  # See : String#methodize, String#pathize
  #
  #   "my_module__my_path".modulize  #=> MyModule::MyPath
  #
  # TODO :
  #
  # * Make sure that all scenarios return a valid ruby class name
  # * Make sure it is revertible
  #
  def modulize
    to_s.gsub(/(__|\/)(.?)/){ "::" + $2.upcase }.gsub(/(^|_)(.)/){ $2.upcase }
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_modulize
      assert_equal( 'MyModule::MyClass',   'my_module__my_class'.modulize   )
      assert_equal( '::MyModule::MyClass', '__my_module__my_class'.modulize )
      assert_equal( 'MyModule::MyClass',   'my_module/my_class'.modulize    )
      assert_equal( '::MyModule::MyClass', '/my_module/my_class'.modulize   )
    end

  end

=end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-1.1.0 lib/facet/string/modulize.rb
facets-1.2.0 lib/facets/core/string/modulize.rb