Sha256: 16af6bff941f2aa92f8c30ccf9977fac8c6e7326a91589272e20f13e50c90179

Contents?: true

Size: 801 Bytes

Versions: 1

Compression:

Stored size: 801 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(string)
      return string unless /[A-Z-]|::/.match?(string)

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

      FastUnderscore.underscore(response)
    end
  end

  class << ActiveSupport::Inflector
    alias as_underscore underscore
    prepend AcronymRegex
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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