While giving room for customizations over this simplest case.
inflector = Dry::Inflector.new do |inflections|
inflections.plural "libro", "libri" # Italian words for "book" and "books"
end
inflector.pluralize("libro") #=> libri
For more complex cases we could use a full blown Inflections object like:
italian = Dry::Inflector::Inflections.new do |inflections|
inflections.plural "libro", "libri"
end
inflector = Dry::Inflector.new(inflections: italian)
inflector.pluralize("libro")
This way, we could provide a built-in set of inflections rules aimed at compatibility with other libraries like:
Dry::Inflector::Inflections::ActiveSupport =
Dry::Inflector::Inflections.new do |inflections|
inflections.plural "person", "people"
end
as_compatible_inflector = Dry::Inflector.new(inflections: Dry::Inflector::Inflections::ActiveSupport)
as_compatible_inflector.pluralize("people") # => "people"
One thing I’m still not sure how to accomplish is composition of inflection rules.
Take the ActiveSupport inflections example:
Dry::Inflector::Inflections::ActiveSupport =
Dry::Inflector::Inflections.new do |inflections|
inflections.plural "person", "people"
end
as_compatible_inflector = Dry::Inflector.new(inflections: Dry::Inflector::Inflections::ActiveSupport)
as_compatible_inflector.pluralize("people") # => "people"
How can I add my own inflection rules on top of the given Dry::Inflector::Inflections::ActiveSupport?
For the simplest case, just providing a block will be enough.
But if I want to stack 2 or 3 different rulesets that I don’t have direct access to their code?
For example Dry::Inflector::Inflections::Italian and Dry::Inflector::Inflections::ActiveSupportCompatible ?
Please let me know if I’m not being clear about it.
By the way, If we define a good way to accomplish this we will also solve internationalization in a roll.
Composition will basically create a new ruleset and copy all rules from given rulesets, so I think we will also need to create some interface for precomposing (and receive something like variant #1)
It’d be nice if we could only expose Dry::Inflector in the public API. So maybe having two methods for buiding up rules + inflector? Dry::Inflector.new { ... } and Dry::Inflector.rules { .. }? Also, passing rules to the .new method sounds like a good idea.