Added to_classname() to string class.
This commit is contained in:
parent
c54dace898
commit
ebea6e3941
2 changed files with 45 additions and 0 deletions
|
@ -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/
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in a new issue