lib/dry/inflector/inflections.rb in dry-inflector-0.1.1 vs lib/dry/inflector/inflections.rb in dry-inflector-0.1.2
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
require "set"
require "dry/inflector/rules"
+require "dry/inflector/acronyms"
module Dry
class Inflector
# Inflections
#
@@ -55,10 +56,18 @@
#
# @since 0.1.0
# @api private
attr_reader :humans
+ # Acronyms
+ #
+ # @return [Dry::Inflector::Acronyms]
+ #
+ # @since 0.1.2
+ # @api private
+ attr_reader :acronyms
+
# Instantiate the rules
#
# @return [Dry::Inflector::Inflections]
# @yieldparam [self]
#
@@ -67,10 +76,11 @@
def initialize
@plurals = Rules.new
@singulars = Rules.new
@humans = Rules.new
@uncountables = Set[]
+ @acronyms = Acronyms.new
yield(self) if block_given?
end
# Add a custom pluralization rule
@@ -156,9 +166,31 @@
# inflections.uncountable "money", "information"
# inflections.uncountable %w(money information rice)
# end
def uncountable(*words)
uncountables.merge(words.flatten)
+ end
+
+ # Add one or more acronyms
+ #
+ # Acronyms affect how basic operations are performed, such
+ # as camelize/underscore.
+ #
+ # @param words [Array<String>] a list of acronyms
+ #
+ # @since 0.1.2
+ #
+ # @example
+ # require "dry/inflector"
+ #
+ # inflector = Dry::Inflector.new do |inflections|
+ # inflections.acronym "HTML"
+ # end
+ #
+ # inflector.camelize("html") # => "HTML"
+ # inflector.underscore("HTMLIsFun") # => "html_is_fun"
+ def acronym(*words)
+ words.each { |word| @acronyms.add(word.downcase, word) }
end
# Add a custom humanize rule
#
# Specifies a humanized form of a string by a regular expression rule or by a string mapping.