diff options
| author | Bobby McDonald <BobbyMcWho@users.noreply.github.com> | 2019-07-18 13:11:41 -0400 |
|---|---|---|
| committer | Michael Herold <opensource@michaeljherold.com> | 2019-07-18 12:11:40 -0500 |
| commit | f0faaca6acc69689aa208c4420c258506a0596af (patch) | |
| tree | 84e43dde19e0968584c40c80f4d1d210fab53648 /lib | |
| parent | da9fd39a0e551e09c1441cb7453c969a4afbfd7f (diff) | |
| download | hashie-f0faaca6acc69689aa208c4420c258506a0596af.tar.gz | |
Add selective key-conflict warnings for Mash subclasses (#478)
In some cases, you want to be able to ignore Mash warnings for keys
that you know you aren't going to access via a method accessor, yet be
warned for other keys that you know you might want to access. This
change adds the ability to selectively ignore warnings for specific keys
instead of globally ignoring the warnings.
The change retains the original behavior as well, so if you call
`Mash.disable_warnings` without a value it will still globally ignore
the warnings.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hashie/mash.rb | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 650a269..06cef59 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -78,8 +78,14 @@ module Hashie # # @api semipublic # @return [void] - def self.disable_warnings + def self.disable_warnings(*method_keys) raise CannotDisableMashWarnings if self == Hashie::Mash + if method_keys.any? + disable_warnings_blacklist.concat(method_keys).tap(&:flatten!).uniq! + else + disable_warnings_blacklist.clear + end + @disable_warnings = true end @@ -87,17 +93,26 @@ module Hashie # # @api semipublic # @return [Boolean] - def self.disable_warnings? + def self.disable_warnings?(method_key = nil) + return disable_warnings_blacklist.include?(method_key) if disable_warnings_blacklist.any? && method_key @disable_warnings ||= false end + # Returns an array of blacklisted methods that this class disables warnings for. + # + # @api semipublic + # @return [Boolean] + def self.disable_warnings_blacklist + @_disable_warnings_blacklist ||= [] + end + # Inheritance hook that sets class configuration when inherited. # # @api semipublic # @return [void] def self.inherited(subclass) super - subclass.disable_warnings if disable_warnings? + subclass.disable_warnings(disable_warnings_blacklist) if disable_warnings? end def self.load(path, options = {}) @@ -346,7 +361,7 @@ module Hashie private def log_built_in_message(method_key) - return if self.class.disable_warnings? + return if self.class.disable_warnings?(method_key) method_information = Hashie::Utils.method_information(method(method_key)) @@ -359,7 +374,7 @@ module Hashie end def log_collision?(method_key) - respond_to?(method_key) && !self.class.disable_warnings? && + respond_to?(method_key) && !self.class.disable_warnings?(method_key) && !(regular_key?(method_key) || regular_key?(method_key.to_s)) end end |
