Sha256: c3ccf33a9e18073b7c41c4e89fda724aa79103edcb466216278fa1005f3fe91a

Contents?: true

Size: 830 Bytes

Versions: 1

Compression:

Stored size: 830 Bytes

Contents

# frozen_string_literal: true

module FastUnderscore
  # Uses ActiveSupport's `acronym_regex` method to construct a memoized pattern
  # for replacing acronyms within strings that need to be underscored.
  module AcronymRegex
    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

    def underscore
      return self unless /[A-Z-]|::/.match?(self)

      response = dup
      response.gsub!(AcronymRegex.pattern) { "#{$1 && '_'}#{$2.downcase}" }

      FastUnderscore.underscore(response)
    end
  end
end

String.prepend(FastUnderscore::AcronymRegex)

class << ActiveSupport::Inflector
  define_method(:underscore, &FastUnderscore.method(:underscore))
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fast_underscore-0.1.0 lib/fast_underscore/ext/acronym_regex.rb