Sha256: 04bbf14517f1350e9ee9c7568c366089ee1be84697bd8f5f8ab8dd30b0f22039

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

class HashGenerator
  VERSION = '1.1.0'

  def initialize
    @value = @scope = {}
    @stack = []
  end

  def end_scope
    @scope = @stack.pop
  end

  def new_array
    begin_scope([])
  end

  def new_object
    begin_scope({})
  end

  def push_object
    new_object
    yield
    push_scope
  end

  def push_scope
    value = @scope
    end_scope
    push(value)
  end

  def reopen_scope(key)
    begin_scope(@scope.fetch(key))
  end

  def store(key, value)
    @scope.store(key, value)
  end

  def store_array(key)
    new_array
    yield
    store_scope(key)
  end

  def store_object(key)
    new_object
    yield
    store_scope(key)
  end

  def store_scope(key)
    value = @scope
    end_scope
    store(key, value)
  end

  def to_h
    @value
  end

  alias to_hash to_h

  private

  def begin_scope(scope)
    @stack.push(@scope)
    @scope = scope
  end

  def push(value)
    @scope.push(value)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_generator-1.1.0 lib/hash_generator.rb