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
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
#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
66 67 68 |
# File 'lib/dry/inflector/inflections.rb', line 66 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
58 59 60 |
# File 'lib/dry/inflector/inflections.rb', line 58 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
34 35 36 |
# File 'lib/dry/inflector/inflections.rb', line 34 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
42 43 44 |
# File 'lib/dry/inflector/inflections.rb', line 42 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
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.
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.
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”`)
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.
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.
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.
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
174 175 176 |
# File 'lib/dry/inflector/inflections.rb', line 174 def uncountable(*words) uncountables.merge(words.flatten) end |