Sha256: 2b0da210c5e0751fa73b085cc71bf60df13eb2bdca5723fbef025733e5045b5a

Contents?: true

Size: 871 Bytes

Versions: 4

Compression:

Stored size: 871 Bytes

Contents

# frozen_string_literal: true

module Dry
  class Inflector
    # A set of acronyms
    #
    # @since 0.1.2
    # @api private
    class Acronyms
      attr_reader :regex

      # @since 0.1.2
      # @api private
      def initialize
        @rules = {}
        define_regex_patterns
      end

      # @since 0.1.2
      # @api private
      def apply_to(word, capitalize: true)
        @rules[word.downcase] || (capitalize ? word.capitalize : word)
      end

      # @since 0.1.2
      # @api private
      def add(rule, replacement)
        @rules[rule] = replacement
        define_regex_patterns
      end

      private

      # @since 0.1.2
      # @api private
      def define_regex_patterns
        regex = @rules.empty? ? /(?=a)b/ : /#{@rules.values.join("|")}/
        @regex = /(?:(?<=([A-Za-z\d]))|\b)(#{regex})(?=\b|[^a-z])/
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-inflector-1.1.0 lib/dry/inflector/acronyms.rb
dry-inflector-1.0.0 lib/dry/inflector/acronyms.rb
dry-inflector-0.3.0 lib/dry/inflector/acronyms.rb
dry-inflector-0.2.1 lib/dry/inflector/acronyms.rb