Module: Wright::Util::ActiveSupport Private
- Defined in:
- lib/wright/util/stolen_from_activesupport.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Various methods copied verbatim from ActiveSupport in order to keep dependencies to a minimum.
Class Method Summary collapse
-
.camelize(s) ⇒ String
private
Converts an underscored_string to UpperCamelCase.
-
.const_regexp(camel_cased_word) ⇒ String
private
Constructs a regular expression that will match a constant part by part.
-
.constantize(camel_cased_word) ⇒ Class
private
Finds a constant with the name specified in the argument string.
-
.safe_constantize(camel_cased_word) ⇒ Class, NilClass
private
Finds a constant with the name specified in the argument string.
-
.underscore(camel_cased_word) ⇒ String
private
Converts a CamelCased string to underscored, lowercase form.
Class Method Details
.camelize(s) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts an underscored_string to UpperCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
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"
86 87 88 89 90 |
# 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 |
.const_regexp(camel_cased_word) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Constructs a regular expression that will match a constant part by part.
193 194 195 196 197 198 199 200 |
# 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 |
.constantize(camel_cased_word) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Replace this method with Module.const_get in Ruby 2.0.
Finds a constant with the name specified in the argument string.
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.
121 122 123 124 125 126 127 128 129 130 131 |
# 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 |
.safe_constantize(camel_cased_word) ⇒ Class, NilClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finds a constant with the name specified in the argument string.
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.
170 171 172 173 174 175 176 177 178 179 180 181 |
# 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) = /#{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) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a CamelCased string to underscored, lowercase form.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold.
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 |