From ebea6e3941b924d6caa08d7f294b8a81828c1b7c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 30 Jun 2015 16:45:03 +0200 Subject: [PATCH] Added to_classname() to string class. --- lib/core_ext/string.rb | 15 +++++++++++++++ test/unit/aaa_string_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/core_ext/string.rb b/lib/core_ext/string.rb index 063221811..24c70d2c7 100644 --- a/lib/core_ext/string.rb +++ b/lib/core_ext/string.rb @@ -35,6 +35,21 @@ class String camel_cased_word.gsub(/::/, '/').downcase end +=begin + + filename = 'some/module.rb'.to_classname + + returns + 'Some::Module' + +=end + + def to_classname + camel_cased_word = "#{self}" + camel_cased_word.gsub!(/\.rb$/, '') + camel_cased_word.split('/').map(&:camelize).join('::') + end + # because of mysql inno_db limitations, strip 4 bytes utf8 chars (e. g. emojis) # unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes # More details: http://pjambet.github.io/blog/emojis-and-mysql/ diff --git a/test/unit/aaa_string_test.rb b/test/unit/aaa_string_test.rb index b9f8b3ca9..5e155ef80 100644 --- a/test/unit/aaa_string_test.rb +++ b/test/unit/aaa_string_test.rb @@ -25,6 +25,36 @@ class AaaStringTest < ActiveSupport::TestCase assert_equal( result, modul.to_filename ) end + test 'to_classname ref' do + modul = 'test' + result = 'test' + modul.to_filename + assert_equal( result, modul ) + + modul = 'some/file' + result = 'some/file' + modul.to_filename + assert_equal( result, modul ) + end + + test 'to_classname function' do + modul = 'test' + result = 'Test' + assert_equal( result, modul.to_classname ) + + modul = 'some/file' + result = 'Some::File' + assert_equal( result, modul.to_classname ) + + modul = 'some/files' + result = 'Some::Files' + assert_equal( result, modul.to_classname ) + + modul = 'some_test/files' + result = 'SomeTest::Files' + assert_equal( result, modul.to_classname ) + end + test 'html2text ref' do html = 'test' result = 'test'