diff options
author | Michael Herold <opensource@michaeljherold.com> | 2020-01-15 13:17:27 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-15 13:17:27 -0600 |
commit | eb69c58b627379568d0c7dfc73eead42c5f1f140 (patch) | |
tree | a977a64010141cb7641624670246c5a6a2f5e64f /lib | |
parent | 25fe2f747ee06e563808e08493ec7a471cc82b26 (diff) | |
download | hashie-eb69c58b627379568d0c7dfc73eead42c5f1f140.tar.gz |
Don't warn when setting most affixed keys (#500)
Due to how we have implemented the bang/underbang/query behavior within
Mash, setting keys that have those affixes in them actually allow
overwriting the behavior of those affixes. As such, we shouldn't warn
when setting a key that matches those patterns.
When it comes to setter-like keys, I believe we still _do_ want to warn
for two reasons:
1. Trying to access the key via method access is a syntax error. Ruby
expects any method ending in `=` to be a 2+-arity method due to the
infix notation of setter methods. This is unexpected behavior unless
you're very familiar with Ruby parsing.
2. You can still retrieve the key via the normal `Hash#[]` reader, but
it prevents setting a similar key without the equal sign. You can see
this in the test about setters. I'd say that is unexpected and
surprising behavior.
Because of these two gotchas, I think we should still warn in cases
where you try to set a key that looks like a setter.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hashie/mash.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 97f75d5..194a6f8 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -406,7 +406,12 @@ module Hashie end def log_collision?(method_key) - respond_to?(method_key) && !self.class.disable_warnings?(method_key) && + return unless respond_to?(method_key) + + _, suffix = method_name_and_suffix(method_key) + + (!suffix || suffix == '='.freeze) && + !self.class.disable_warnings?(method_key) && !(regular_key?(method_key) || regular_key?(method_key.to_s)) end end |