summaryrefslogtreecommitdiff
path: root/lib/hashie/mash.rb
diff options
context:
space:
mode:
authorMichael Herold <michael.j.herold@gmail.com>2017-02-24 07:13:12 -0600
committerMichael Herold <michael.j.herold@gmail.com>2017-02-24 07:47:52 -0600
commit55cfb8adbcaf7d9ca27bc630cde06ca6430e8607 (patch)
tree856374fc46ae79f796c5aa35bae3c45288257bd6 /lib/hashie/mash.rb
parent9f77380ddbc72347063065d9e9f7c4a13cd504d5 (diff)
downloadhashie-55cfb8adbcaf7d9ca27bc630cde06ca6430e8607.tar.gz
Don't log when overwriting Mash keys
When we switched to using `#respond_to?` to detecting whether to log a Mash collision, we started reporting when we were overwriting keys that already exist in the Mash. This is a poor experience because it causes extra warnings (as in #414) or, in the worst case, causes an "undefined method" error (as in #413). This change fixes that problem and benchmarks to ensure we're not appreciably regressing performance. The results of two benchmarks are below: ``` bundle exec ruby benchmark/mash_method_access.rb: Warming up -------------------------------------- before 92.456k i/100ms Calculating ------------------------------------- before 1.290M (± 4.4%) i/s - 6.472M in 5.028183s Pausing here -- run Ruby again to measure the next benchmark... Warming up -------------------------------------- after 92.941k i/100ms Calculating ------------------------------------- after 1.326M (± 5.4%) i/s - 6.692M in 5.060756s Comparison: after: 1326239.2 i/s before: 1289624.0 i/s - same-ish: difference falls within error ``` and ``` within spec/integrations/omniauth, bundle exec rake perf:ips Warming up -------------------------------------- before 1.260k i/100ms Calculating ------------------------------------- before 13.114k (± 4.2%) i/s - 66.780k in 5.101689s Pausing here -- run Ruby again to measure the next benchmark... Warming up -------------------------------------- after 1.299k i/100ms Calculating ------------------------------------- after 13.149k (± 4.0%) i/s - 66.249k in 5.046630s Comparison: after: 13148.9 i/s before: 13113.8 i/s - same-ish: difference falls within error ``` Closes #413 Closes #414
Diffstat (limited to 'lib/hashie/mash.rb')
-rw-r--r--lib/hashie/mash.rb7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index cc8cb20..390a731 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -145,7 +145,7 @@ module Hashie
def custom_writer(key, value, convert = true) #:nodoc:
key_as_symbol = (key = convert_key(key)).to_sym
- log_built_in_message(key_as_symbol) if respond_to?(key_as_symbol)
+ log_built_in_message(key_as_symbol) if log_collision?(key_as_symbol)
regular_writer(key, convert ? convert_value(value) : value)
end
@@ -348,5 +348,10 @@ module Hashie
'property. You can still access the key via the #[] method.'
)
end
+
+ def log_collision?(method_key)
+ respond_to?(method_key) && !self.class.disable_warnings? &&
+ !(regular_key?(method_key) || regular_key?(method_key.to_s))
+ end
end
end