Module: Wright::Util::ActiveSupport
- Defined in:
- lib/wright/util/stolen_from_activesupport.rb
Overview
Internal: Various methods copied verbatim from ActiveSupport in
order to keep dependencies to a minimum.
Class Method Summary collapse
-
.camelize(s) ⇒ Object
Internal: Convert an underscored_string to UpperCamelCase.
-
.const_regexp(camel_cased_word) ⇒ Object
Internal: Construct a regular expression that will match a constant part by part.
-
.constantize(camel_cased_word) ⇒ Object
Internal: Find a constant with the name specified in the argument string.
-
.safe_constantize(camel_cased_word) ⇒ Object
Internal: Find a constant with the name specified in the argument string.
-
.underscore(camel_cased_word) ⇒ Object
Internal: Convert a CamelCased String to underscored, lowercase form.
Class Method Details
.camelize(s) ⇒ Object
Internal: Convert an underscored_string to UpperCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for
converting paths to namespaces.
s - The String to be camelized.
Examples
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"
Returns the camelized String.
87 88 89 90 91 |
# File 'lib/wright/util/stolen_from_activesupport.rb', line 87 def self.camelize(s) s.to_s .gsub(/\/(.?)/) { "::#{Regexp.last_match[1].upcase}" } .gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase } end |
.const_regexp(camel_cased_word) ⇒ Object
Internal: Construct a regular expression that will match a constant part by part.
camel_cased_word - The CamelCased String constant name.
Examples
const_regexp("Foo::Bar::Baz")
# => "Foo(::Bar(::Baz)?)?"
Returns the String to be used as a regular expression.
192 193 194 195 196 197 198 199 |
# File 'lib/wright/util/stolen_from_activesupport.rb', line 192 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 |
.constantize(camel_cased_word) ⇒ Object
Internal: Find a constant with the name specified in the argument string.
camel_cased_word - The String name of the constant to find.
Examples
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:
C = 'outside'
module M
C = 'inside'
C # => 'inside'
constantize("C") # => 'outside', same as ::C
end
Returns the constant. Raises NameError if the constant name is not in CamelCase or
the constant is unknown.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/wright/util/stolen_from_activesupport.rb', line 120 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 |
.safe_constantize(camel_cased_word) ⇒ Object
Internal: Find a constant with the name specified in the argument string.
camel_cased_word - The String name of the constant to find.
Examples
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:
C = 'outside'
module M
C = 'inside'
C # => 'inside'
constantize("C") # => 'outside', same as ::C
end
nil is returned when the name is not in CamelCase or the constant (or part of it) is unknown.
safe_constantize("blargle")
# => nil
safe_constantize("UnknownModule")
# => nil
safe_constantize("UnknownModule::Foo::Bar")
# => nil
Returns the constant or nil if the name is not in CamelCase or
the constant is unknown.
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/wright/util/stolen_from_activesupport.rb', line 170 def self.safe_constantize(camel_cased_word) constantize(camel_cased_word) rescue NameError => e error_re = /(uninitialized constant|wrong constant name)/ const_re = const_regexp(camel_cased_word) = /#{error_re} #{const_re}$/ uninitialized_constant_exception = e. =~ || e.name.to_s == camel_cased_word.to_s raise unless uninitialized_constant_exception end |
.underscore(camel_cased_word) ⇒ Object
Internal: Convert a CamelCased String to underscored,
lowercase form.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
camel_cased_word - The String to be underscored.
Examples
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:
camelize(underscore("SSLError"))
# => "SslError"
Returns the underscored String.
55 56 57 58 59 60 61 62 63 |
# 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 |