Class: Dry::Inflector::Inflections

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/inflector/inflections.rb,
lib/dry/inflector/inflections/defaults.rb

Overview

Inflections

Since:

  • 0.1.0

Defined Under Namespace

Modules: Defaults

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|| ... } ⇒ Dry::Inflector::Inflections

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate the rules

Yield Parameters:

  • (self)

Since:

  • 0.1.0



76
77
78
79
80
81
82
83
84
# File 'lib/dry/inflector/inflections.rb', line 76

def initialize
  @plurals      = Rules.new
  @singulars    = Rules.new
  @humans       = Rules.new
  @uncountables = Set[]
  @acronyms     = Acronyms.new

  yield(self) if block_given?
end

Instance Attribute Details

#acronymsDry::Inflector::Acronyms (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Acronyms

Returns:

Since:

  • 0.1.2



67
68
69
# File 'lib/dry/inflector/inflections.rb', line 67

def acronyms
  @acronyms
end

#humansDry::Inflector::Rules (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Human rules

Returns:

Since:

  • 0.1.0



59
60
61
# File 'lib/dry/inflector/inflections.rb', line 59

def humans
  @humans
end

#pluralsDry::Inflector::Rules (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pluralization rules

Returns:

Since:

  • 0.1.0



35
36
37
# File 'lib/dry/inflector/inflections.rb', line 35

def plurals
  @plurals
end

#singularsDry::Inflector::Rules (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Singularization rules

Returns:

Since:

  • 0.1.0



43
44
45
# File 'lib/dry/inflector/inflections.rb', line 43

def singulars
  @singulars
end

#uncountablesSet (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Uncountable rules

Returns:

  • (Set)

Since:

  • 0.1.0



51
52
53
# File 'lib/dry/inflector/inflections.rb', line 51

def uncountables
  @uncountables
end

Class Method Details

.build(&blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate a set of inflection rules. It adds the default rules and the optional customizations, passed as a block.

Parameters:

  • blk (Proc)

    the optional, custom rules

Since:

  • 0.1.0



22
23
24
25
26
27
# File 'lib/dry/inflector/inflections.rb', line 22

def self.build(&blk)
  new do |inflect|
    Defaults.call(inflect)
    blk.call(inflect) if block_given?
  end
end

Instance Method Details

#acronym(*words) ⇒ Object

Add one or more acronyms

Acronyms affect how basic operations are performed, such as camelize/underscore.

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.acronym "HTML"
end

inflector.camelize("html")        # => "HTML"
inflector.underscore("HTMLIsFun") # => "html_is_fun"

Parameters:

  • words (Array<String>)

    a list of acronyms

Since:

  • 0.1.2



197
198
199
# File 'lib/dry/inflector/inflections.rb', line 197

def acronym(*words)
  words.each { |word| @acronyms.add(word.downcase, word) }
end

#human(rule, replacement) ⇒ Object

Add a custom humanize rule

Specifies a humanized form of a string by a regular expression rule or by a string mapping.

When using a regular expression based replacement, the normal humanize formatting is called after the replacement.

When a string is used, the human form should be specified as desired (example: ‘“The name”`, not `“the_name”`)

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.human(/_cnt$/i, '\1_count')
  inflections.human("legacy_col_person_name", "Name")
end

Parameters:

  • rule (String, Regexp)

    the rule

  • replacement (String)

    the replacement

Since:

  • 0.1.0



224
225
226
# File 'lib/dry/inflector/inflections.rb', line 224

def human(rule, replacement)
  humans.insert(0, [rule, replacement])
end

#irregular(singular, plural) ⇒ Object

Add a custom pluralization rule

Specifies a new irregular that applies to both pluralization and singularization at the same time.

This can only be used for strings, not regular expressions. You simply pass the irregular in singular and plural form.

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.singular "octopus", "octopi"
end

Parameters:

  • singular (String)

    the singular

  • plural (String)

    the plural

Since:

  • 0.1.0



151
152
153
154
155
156
157
# File 'lib/dry/inflector/inflections.rb', line 151

def irregular(singular, plural)
  uncountables.delete(singular)
  uncountables.delete(plural)

  add_irregular(singular, plural, plurals)
  add_irregular(plural, singular, singulars)
end

#plural(rule, replacement) ⇒ Object

Add a custom pluralization rule

Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.

The replacement should always be a string that may include references to the matched data from the rule.

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.plural "virus", "viruses"
end

Parameters:

  • rule (String, Regexp)

    the rule

  • replacement (String)

    the replacement

Since:

  • 0.1.0



105
106
107
# File 'lib/dry/inflector/inflections.rb', line 105

def plural(rule, replacement)
  rule(rule, replacement, plurals)
end

#singular(rule, replacement) ⇒ Object

Add a custom singularization rule

Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.

The replacement should always be a string that may include references to the matched data from the rule.

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.singular "thieves", "thief"
end

Parameters:

  • rule (String, Regexp)

    the rule

  • replacement (String)

    the replacement

Since:

  • 0.1.0



128
129
130
# File 'lib/dry/inflector/inflections.rb', line 128

def singular(rule, replacement)
  rule(rule, replacement, singulars)
end

#uncountable(*words) ⇒ Object

Add a custom rule for uncountable words

Uncountable will not be inflected

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.uncountable "money"
  inflections.uncountable "money", "information"
  inflections.uncountable %w(money information rice)
end

Parameters:

  • words (Enumerable<String>)

Since:

  • 0.1.0



175
176
177
# File 'lib/dry/inflector/inflections.rb', line 175

def uncountable(*words)
  uncountables.merge(words.flatten)
end