Dry-Inflector defaults

Hello!

I want to discuss some significant change in dry-inflector before it will reach 1.0 (or will appear in Hanami 2 release)

Here is a problem: as a support library, which is mainly used for inflecting routes and database tables, dry-inflector have a lot of defaults which is not very related to this task and in fact is very rarely used. I don’t think there is many database table or routes with the “octopus” name for example. Hovewer, having defaults slows down many inflections linearely, but at the same time doesn’t cover even 10% of cases I think. The task of processing human language texts is out of context of this library and should be done in a very different way.

So, my proposal is to deprecate loading defaults, cut the existing and maybe split them into categories if needed. There was some proposals for the rulesets API here, Dry::Inflector future api - #2 by abinoam but I also have some ideas of how to not expose additional constants if needed

1 Like

Hey, this sounds like a good idea. In Hanami we’d probably enable sane defaults anyway, but dry-inflector standalone should be slim and fast by default.

I just realized that defaults indeed should be provided by higher level framework, so it makes sense to just ditch all the rules (except, maybe, basic ones with adding “s”) from dry-inflector itself.

However, we need an API to be able to ship an inflector with framework defaults but with ability for later configuration. I see few ways of doing so:

  1. Ruleset composition, as suggested in a topic mentioned above:
HanamiDefaults = Dry::Inflector.ruleset { ... }
RomDefaults = Dry::Inflector.ruleset { ... }

config.inflector = Dry::Inflector.new(HanamiDefaults | RomDefaults, &block) 
  1. Inheritance:
config.inflector = Dry::Inflector.new { ... }
db.inflector = Dry::Inflector.inherit(config.inflector) { ... }

which may be useful for reusing system-wide inflector for specific needs (autoloader, db, router, etc.)

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

This one may be useful for user-defined external rulesets, shipped with 3rd-party libs but seems overthought a bit

Can’t we just have this:

hanami_inflector = Dry.Inflector { ... }
rom_inflector = Dry.Inflector { ... }

my_inflector = hanami_inflector.merge(rom_inflector)

?

Looks good, will try to PR soon