Project

General

Profile

Bug #11402 ยป data_hash.rb

laise (Alexey Chernenkov), 07/31/2015 07:39 AM

 
1
module SummaryStats
2
  class AggregateTxService
3
    class DataHash
4
      attr_reader :h
5

    
6
      def initialize(&default)
7
        @h = {}
8
        @d = default
9
      end
10

    
11
      def get(*opts)
12
        assert opts.any?, "At least one opt must be passed"
13
        x = @h
14
        opts.each_with_index do |opt, i|
15
          k = opt.to_s
16
          unless x.has_key?(k)
17
            if i < opts.size - 1
18
              x[k] = {}
19
            else
20
              x[k] = @d.call
21
            end
22
          end
23
          x = x[k]
24
        end
25
        x
26
      end
27

    
28
      def set(*opts, &block)
29
        x = get(*opts)
30
        yield x
31
        return self
32
      end
33
    end
34
  end
35
end