Module: FastUnderscore::AcronymRegex
- Defined in:
- lib/fast_underscore/acronym_regex.rb
Overview
Uses ActiveSupport’s ‘acronym_regex` method to construct a memoized pattern for replacing acronyms within strings that need to be underscored.
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.pattern ⇒ Object
7 8 9 10 11 12 |
# File 'lib/fast_underscore/acronym_regex.rb', line 7 def self.pattern return @pattern if defined?(@pattern) acronym_regex = ActiveSupport::Inflector.inflections.acronym_regex @pattern ||= /(?:(?<=([A-Za-z\d]))|\b)(#{acronym_regex})(?=\b|[^a-z])/ end |
Instance Method Details
#underscore(string) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/fast_underscore/acronym_regex.rb', line 14 def underscore(string) return string unless /[A-Z-]|::/.match?(string) response = string.dup response.gsub!(AcronymRegex.pattern) { "#{$1 && '_'}#{$2.downcase}" } FastUnderscore.underscore(response) end |