Sha256: 220738b7900afa8db44cae715d5e931b5f2b40cb59a2ed296e0a170f62c6c9d7

Contents?: true

Size: 952 Bytes

Versions: 12

Compression:

Stored size: 952 Bytes

Contents

# Copied from ActiveSupport so we don't have to add ActiveSupport as a dependency just for this method.
class String
  # Makes an underscored, lowercase form from the expression in the string.
  #
  # Changes '::' to '/' to convert namespaces to paths.
  #
  #   'ActiveModel'.underscore         # => "active_model"
  #   'ActiveModel::Errors'.underscore # => "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:
  #
  #   'SSLError'.underscore.camelize # => "SslError"
  def underscore
    return self unless self =~ /[A-Z-]|::/
    word = self.to_s.gsub(/::/, '/')
    # word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'}#{$2.downcase}" }
    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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
aws_metadata-0.3.0 lib/extensions/string.rb
aws_metadata-0.2.4 lib/extensions/string.rb
aws_metadata-0.2.3 lib/extensions/string.rb
aws_metadata-0.2.2 lib/extensions/string.rb
aws_metadata-0.2.1 lib/extensions/string.rb
aws_metadata-0.2.0 lib/extensions/string.rb
aws_metadata-0.1.5 lib/extensions/string.rb
aws_metadata-0.1.4 lib/extensions/string.rb
aws_metadata-0.1.3 lib/extensions/string.rb
aws_metadata-0.1.2 lib/extensions/string.rb
aws_metadata-0.1.1 lib/extensions/string.rb
aws_metadata-0.1.0 lib/extensions/string.rb