clio / polymorphic_integer_type

MIT License
37 stars 35 forks source link

Add a mechanism to help prevent collisions #9

Open doliveirakn opened 10 years ago

doliveirakn commented 10 years ago

Right now you can create collisions by doing something like

config.add :actor, {
  1 => 'A',
  1 => 'B'
}

If you did something like this, all references to 'A' will become references to 'B' and that could be catastrophic. A mechanism should be added so that this is prevented at a code level.

fmluizao commented 6 years ago

What if instead of a hash we use an array to define mappings? This way, we can iterate on that array, and check for duplicate keys. Not as clean as using a hash, and breaks the current API, but solves the problem:

config.add :actor, [
  [1, 'A'],
  [1, 'B']
]

Using hash its impossible to detect this. Ruby emits a warning, but I think that's no way to handle this...

x = {1=> 'A', 1=> 'B'}
warning: key 1 is duplicated and overwritten on line 3