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

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"

Examples:

camelize("active_record")
# => "ActiveRecord"

camelize("active_record/errors")
# => "ActiveRecord::Errors"

Parameters:

  • s (String)

    the string to be camelized

Returns:

  • (String)

    the camelized string



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.

Examples:

const_regexp("Foo::Bar::Baz")
# => "Foo(::Bar(::Baz)?)?"

Parameters:

  • camel_cased_word (String)

    the CamelCased constant name

Returns:

  • (String)

    the string to be used as a regular expression



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.

TODO:

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.

Examples:

constantize("Module")
# => Module

constantize("Test::Unit")
# => Test::Unit
C = 'outside'
module M
  C = 'inside'
  C                # => 'inside'
  constantize("C") # => 'outside', same as ::C
end

Parameters:

  • camel_cased_word (String)

    the name of the constant to find

Returns:

  • (Class)

    the constant

Raises:

  • (NameError)

    if the constant name is not in CamelCase or the constant is unknown



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.

Examples:

constantize("Module")
# => Module

constantize("Test::Unit")
# => Test::Unit
C = 'outside'
module M
  C = 'inside'
  C                # => 'inside'
  constantize("C") # => 'outside', same as ::C
end
safe_constantize("blargle")
# => nil

safe_constantize("UnknownModule")
# => nil

safe_constantize("UnknownModule::Foo::Bar")
# => nil

Parameters:

  • camel_cased_word (String)

    the name of the constant to find

Returns:

  • (Class, NilClass)

    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
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)
  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

.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.

Examples:

underscore("ActiveModel")
# => "active_model"

underscore("ActiveModel::Errors")
# => "active_model/errors"
camelize(underscore("SSLError"))
# => "SslError"

Parameters:

  • camel_cased_word (String)

    the string to be converted

Returns:

  • (String)

    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