Module: RSpecMagic::Stable::DescribedSym::Util

Defined in:
lib/rspec_magic/stable/described_sym.rb

Overview

Utilities.

Class Method Summary collapse

Class Method Details

.underscore(camel_cased_word) ⇒ Symbol

Generate an underscored, lowercase representation of a camel-cased word. "::"s are converted to "/"s.

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

NOTE: This method has been ported from ActiveSupport, mostly as is.

Parameters:

  • camel_cased_word (String)

Returns:

  • (Symbol)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rspec_magic/stable/described_sym.rb', line 42

def self.underscore(camel_cased_word)
  # Unlike ActiveSupport, we don't support acronyms.
  acronym_regex = /(?=a)b/

  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$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