summaryrefslogtreecommitdiff
path: root/lib/hashie/extensions/mash/symbolize_keys.rb
blob: 91a6b1472d6752f5ba85e605daac4ad8e072a0c2 (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
module Hashie
  module Extensions
    module Mash
      # Overrides Mash's default behavior of converting keys to strings
      #
      # @example
      #   class LazyResponse < Hashie::Mash
      #     include Hashie::Extensions::Mash::SymbolizedKeys
      #   end
      #
      #   response = LazyResponse.new("id" => 123, "name" => "Rey").to_h
      #   #=> {id: 123, name: "Rey"}
      #
      # @api public
      module SymbolizeKeys
        # Hook for being included in a class
        #
        # @api private
        # @return [void]
        # @raise [ArgumentError] when the base class isn't a Mash
        def self.included(base)
          raise ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash
        end

        private

        # Converts a key to a symbol
        #
        # @api private
        # @param [String, Symbol] key the key to convert to a symbol
        # @return [void]
        def convert_key(key)
          key.to_sym
        end
      end
    end
  end
end