Sha256: 691a57dc4dd24c77017a2a492c064f1053d60a3014193d01095630d232d4b23c

Contents?: true

Size: 993 Bytes

Versions: 5

Compression:

Stored size: 993 Bytes

Contents

module Dcha
  module MPT
    class Node < Array
      # :nodoc:
      module ToHashable
        def to_h
          case type
          when :blank then {}
          when :branch then branch_to_h
          when :leaf, :extension
            kv_to_h
          end
        end

        private

        def branch_to_h
          hash = {}
          16.times do |i|
            sub_hash = Node.decode(self[i]).to_h
            sub_hash.each { |k, v| hash[NibbleKey.new([i]) + k] = v }
          end
          hash[NibbleKey.terminator] = last if last
          hash
        end

        def kv_to_h
          nibbles = NibbleKey.decode(first).terminate(false)
          sub_hash = if type == :extension
                       Node.decode(self[1]).to_h
                     else
                       { NibbleKey.terminator => self[1] }
                     end

          {}.tap do |h|
            sub_hash.each { |k, v| h[nibbles + k] = v }
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dcha-0.1.4 lib/dcha/mpt/node/to_hashable.rb
dcha-0.1.3 lib/dcha/mpt/node/to_hashable.rb
dcha-0.1.2 lib/dcha/mpt/node/to_hashable.rb
dcha-0.1.1 lib/dcha/mpt/node/to_hashable.rb
dcha-0.1.0 lib/dcha/mpt/node/to_hashable.rb