module Autoloaded::Inflection
Translates source filenames into constants.
@since 1.3
@api private
Public Class Methods
to_constant_name(source_filename)
click to toggle source
Translates a String representing a source filename into a Symbol representing a constant.
@param [String] source_filename the name of a source code file
@return [Symbol] the name of a constant corresponding to source_filename
@raise [ArgumentError] source_filename is nil
or empty
@note Directories are ignored rather than translated into namespaces.
# File lib/autoloaded/inflection.rb, line 18 def self.to_constant_name(source_filename) source_filename = source_filename.to_s raise(::ArgumentError, "can't be blank") if source_filename.empty? translate(source_filename, *[:file_basename, :camelize_if_lowercase, :nonalphanumeric_to_underscore, :delete_leading_nonalphabetic, :capitalize_first]).to_sym end
Private Class Methods
camelize(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 31 def self.camelize(string) string.gsub(/(.)(?:_|-)+(.)/) do |match| "#{match[0..0].downcase}#{match[-1..-1].upcase}" end end
camelize_if_lowercase(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 37 def self.camelize_if_lowercase(string) return string unless lowercase?(string) camelize string end
capitalize_first(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 43 def self.capitalize_first(string) "#{string[0..0].upcase}#{string[1..-1]}" end
delete_leading_nonalphabetic(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 47 def self.delete_leading_nonalphabetic(string) string.gsub(/^[^a-z]+/i, '') end
file_basename(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 51 def self.file_basename(string) ::File.basename string, ::File.extname(string) end
lowercase?(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 55 def self.lowercase?(string) string == string.downcase end
nonalphanumeric_to_underscore(string)
click to toggle source
# File lib/autoloaded/inflection.rb, line 59 def self.nonalphanumeric_to_underscore(string) string.gsub(/[^a-z0-9]+/i, '_') end
translate(string, *translations)
click to toggle source
# File lib/autoloaded/inflection.rb, line 63 def self.translate(string, *translations) translations.inject string do |result, translation| send translation, result end end