diff options
Diffstat (limited to 'lib/hashie/extensions/key_conflict_warning.rb')
-rw-r--r-- | lib/hashie/extensions/key_conflict_warning.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/hashie/extensions/key_conflict_warning.rb b/lib/hashie/extensions/key_conflict_warning.rb new file mode 100644 index 0000000..7ce56a1 --- /dev/null +++ b/lib/hashie/extensions/key_conflict_warning.rb @@ -0,0 +1,55 @@ +module Hashie + module Extensions + module KeyConflictWarning + class CannotDisableMashWarnings < StandardError + def initialize + super( + 'You cannot disable warnings on the base Mash class. ' \ + 'Please subclass the Mash and disable it in the subclass.' + ) + end + end + + # Disable the logging of warnings based on keys conflicting keys/methods + # + # @api semipublic + # @return [void] + def disable_warnings(*method_keys) + raise CannotDisableMashWarnings if self == Hashie::Mash + if method_keys.any? + disabled_warnings.concat(method_keys).tap(&:flatten!).uniq! + else + disabled_warnings.clear + end + + @disable_warnings = true + end + + # Checks whether this class disables warnings for conflicting keys/methods + # + # @api semipublic + # @return [Boolean] + def disable_warnings?(method_key = nil) + return disabled_warnings.include?(method_key) if disabled_warnings.any? && method_key + @disable_warnings ||= false + end + + # Returns an array of blacklisted methods that this class disables warnings for. + # + # @api semipublic + # @return [Boolean] + def disabled_warnings + @_disabled_warnings ||= [] + end + + # Inheritance hook that sets class configuration when inherited. + # + # @api semipublic + # @return [void] + def inherited(subclass) + super + subclass.disable_warnings(disabled_warnings) if disable_warnings? + end + end + end +end |