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

API:

  • private



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

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

API:

  • private



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

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

API:

  • private



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

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

API:

  • private



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

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

API:

  • private



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

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:

Since:

  • 0.1.0

API:

  • private



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

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:

  • the optional, custom rules

Since:

  • 0.1.0

API:

  • private



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

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:

  • a list of acronyms

Since:

  • 0.1.2



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

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:

  • the rule

  • the replacement

Since:

  • 0.1.0



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

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:

  • the singular

  • the plural

Since:

  • 0.1.0



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

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:

  • the rule

  • the replacement

Since:

  • 0.1.0



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

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:

  • the rule

  • the replacement

Since:

  • 0.1.0



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

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:

Since:

  • 0.1.0



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

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