summaryrefslogtreecommitdiff
path: root/lib/hashie/extensions/merge_initializer.rb
blob: 19a9d28a8d35282e64dc293efa315ed2afa1aee9 (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
module Hashie
  module Extensions
    # The MergeInitializer is a super-simple mixin that allows
    # you to initialize a subclass of Hash with another Hash
    # to give you faster startup time for Hash subclasses. Note
    # that you can still provide a default value as a second
    # argument to the initializer.
    #
    # @example
    #   class MyHash < Hash
    #     include Hashie::Extensions::MergeInitializer
    #   end
    #
    #   h = MyHash.new(:abc => 'def')
    #   h[:abc] # => 'def'
    #
    module MergeInitializer
      def initialize(hash = {}, default = nil, &block)
        default ? super(default) : super(&block)
        hash.each do |key, value|
          self[key] = value
        end
      end
    end
  end
end