Class: Dry::Inflector::Inflections
- Inherits:
-
Object
- Object
- Dry::Inflector::Inflections
- Defined in:
- lib/dry/inflector/inflections.rb,
lib/dry/inflector/inflections/defaults.rb
Overview
Inflections
Defined Under Namespace
Modules: Defaults
Instance Attribute Summary collapse
-
#acronyms ⇒ Dry::Inflector::Acronyms
readonly
private
Acronyms.
-
#humans ⇒ Dry::Inflector::Rules
readonly
private
Human rules.
-
#plurals ⇒ Dry::Inflector::Rules
readonly
private
Pluralization rules.
-
#singulars ⇒ Dry::Inflector::Rules
readonly
private
Singularization rules.
-
#uncountables ⇒ Set
readonly
private
Uncountable rules.
Class Method Summary collapse
-
.build(&blk) ⇒ Object
private
Instantiate a set of inflection rules.
Instance Method Summary collapse
-
#acronym(*words) ⇒ Object
Add one or more acronyms.
-
#human(rule, replacement) ⇒ Object
Add a custom humanize rule.
-
#initialize {|| ... } ⇒ Dry::Inflector::Inflections
constructor
private
Instantiate the rules.
-
#irregular(singular, plural) ⇒ Object
Add a custom pluralization rule.
-
#plural(rule, replacement) ⇒ Object
Add a custom pluralization rule.
-
#singular(rule, replacement) ⇒ Object
Add a custom singularization rule.
-
#uncountable(*words) ⇒ Object
Add a custom rule for uncountable words.
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
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
#acronyms ⇒ Dry::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
67 68 69 |
# File 'lib/dry/inflector/inflections.rb', line 67 def acronyms @acronyms end |
#humans ⇒ Dry::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
59 60 61 |
# File 'lib/dry/inflector/inflections.rb', line 59 def humans @humans end |
#plurals ⇒ Dry::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
35 36 37 |
# File 'lib/dry/inflector/inflections.rb', line 35 def plurals @plurals end |
#singulars ⇒ Dry::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
43 44 45 |
# File 'lib/dry/inflector/inflections.rb', line 43 def singulars @singulars end |
#uncountables ⇒ Set (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
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.
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.
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”`)
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.
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.
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.
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
175 176 177 |
# File 'lib/dry/inflector/inflections.rb', line 175 def uncountable(*words) uncountables.merge(words.flatten) end |