summaryrefslogtreecommitdiff
path: root/lib/hashie/extensions/key_conflict_warning.rb
blob: 7ce56a17e24c86bf1c93d7ba80adf659fa460e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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