module Wright::Util::ActiveSupport
@api private Various methods copied verbatim from ActiveSupport
in order to keep dependencies to a minimum.
Public Class Methods
Converts an underscored_string to UpperCamelCase.
camelize will also convert '/' to '::' which is useful for converting paths to namespaces.
@param s [String] the string to be camelized
@example
camelize("active_record") # => "ActiveRecord" camelize("active_record/errors") # => "ActiveRecord::Errors"
As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold.
camelize(underscore("SSLError")) # => "SslError"
@return [String] the camelized string
# File lib/wright/util/stolen_from_activesupport.rb, line 86 def self.camelize(s) s.to_s .gsub(%r{/(.?)}) { "::#{Regexp.last_match[1].upcase}" } .gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase } end
Constructs a regular expression that will match a constant part by part.
@param camel_cased_word [String] the CamelCased constant name
@example
const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?"
@return [String] the string to be used as a regular expression
# File lib/wright/util/stolen_from_activesupport.rb, line 193 def self.const_regexp(camel_cased_word) parts = camel_cased_word.split('::') last = parts.pop parts.reverse.reduce(last) do |acc, part| part.empty? ? acc : "#{part}(::#{acc})?" end end
Finds a constant with the name specified in the argument string.
@param camel_cased_word [String] the name of the constant to
find
@example
constantize("Module") # => Module constantize("Test::Unit") # => Test::Unit
The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account.
@example
C = 'outside' module M C = 'inside' C # => 'inside' constantize("C") # => 'outside', same as ::C end
@return [Class] the constant @raise [NameError] if the constant name is not in CamelCase or
the constant is unknown
@todo Replace this method with Module.const_get in Ruby 2.0.
# File lib/wright/util/stolen_from_activesupport.rb, line 121 def self.constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? c = Object names.each do |name| const_defined = c.const_defined?(name, false) c = const_defined ? c.const_get(name) : c.const_missing(name) end c end
Finds a constant with the name specified in the argument string.
@param camel_cased_word [String] the name of the constant to
find
@example
constantize("Module") # => Module constantize("Test::Unit") # => Test::Unit
The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account.
@example
C = 'outside' module M C = 'inside' C # => 'inside' constantize("C") # => 'outside', same as ::C end
@example
safe_constantize("blargle") # => nil safe_constantize("UnknownModule") # => nil safe_constantize("UnknownModule::Foo::Bar") # => nil
@return [Class, NilClass] the constant or nil if the name is
not in CamelCase or the constant is unknown
# File lib/wright/util/stolen_from_activesupport.rb, line 170 def self.safe_constantize(camel_cased_word) return nil if camel_cased_word.nil? constantize(camel_cased_word) rescue NameError => e error_re = /(uninitialized constant|wrong constant name)/ const_re = const_regexp(camel_cased_word) message_re = /#{error_re} #{const_re}$/ uninitialized_constant_exception = e.message =~ message_re || e.name.to_s == camel_cased_word.to_s raise unless uninitialized_constant_exception end
Converts a CamelCased string to underscored, lowercase form.
Changes '::' to '/' to convert namespaces to paths.
@param camel_cased_word [String] the string to be converted
@example
underscore("ActiveModel") # => "active_model" underscore("ActiveModel::Errors") # => "active_model/errors"
As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold.
@example
camelize(underscore("SSLError")) # => "SslError"
@return [String] the underscored string
# File lib/wright/util/stolen_from_activesupport.rb, line 55 def self.underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr!('-', '_') word.downcase! word end